001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.container;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    import java.net.URL;
022    import java.util.Enumeration;
023    import java.util.Properties;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.servicemix.jbi.framework.AdminCommandsService;
028    import org.springframework.beans.factory.InitializingBean;
029    
030    /**
031     * @version $Revision: 1.1 $
032     */
033    public abstract class DeploySupport implements InitializingBean {
034        private static final transient Log LOG = LogFactory.getLog(DeploySupport.class);
035    
036        private JBIContainer jbiContainer;
037        private AdminCommandsService commandsService;
038        private boolean deferException;
039        private String homeDir;
040        private String repositoryDir;
041        private String groupId;
042        private String artifactId;
043        private String version;
044        private String type = "-installer.zip";
045        private String file;
046    
047        public void afterPropertiesSet() throws Exception {
048        }
049    
050        public void deploy(JBIContainer container) throws Exception {
051            setJbiContainer(container);
052            if (container == null) {
053                throw new IllegalArgumentException("No JBI container configured!");
054            }
055            if (getCommandsService() == null) {
056                setCommandsService(getJbiContainer().getAdminCommandsService());
057            }
058            doDeploy();
059        }
060    
061    
062        // Properties
063        //-------------------------------------------------------------------------
064    
065        public JBIContainer getJbiContainer() {
066            return jbiContainer;
067        }
068    
069        public void setJbiContainer(JBIContainer jbiContainer) {
070            this.jbiContainer = jbiContainer;
071        }
072    
073        public AdminCommandsService getCommandsService() {
074            return commandsService;
075        }
076    
077        public void setCommandsService(AdminCommandsService commandsService) {
078            this.commandsService = commandsService;
079        }
080    
081        public boolean isDeferException() {
082            return deferException;
083        }
084    
085        public void setDeferException(boolean deferException) {
086            this.deferException = deferException;
087        }
088    
089        public String getArtifactId() {
090            if (artifactId == null) {
091                throw new IllegalArgumentException("You must specify either a file or a groupId and an artifactId property");
092            }
093            return artifactId;
094        }
095    
096        public void setArtifactId(String artifactId) {
097            this.artifactId = artifactId;
098        }
099    
100        public String getGroupId() {
101            if (groupId == null) {
102                throw new IllegalArgumentException("You must specify either a file or a groupId and an artifactId property");
103            }
104            return groupId;
105        }
106    
107        public void setGroupId(String groupId) {
108            this.groupId = groupId;
109        }
110    
111        public String getHomeDir() {
112            if (homeDir == null) {
113                homeDir = System.getProperty("user.home", "~");
114                String os = System.getProperty("os.name");
115                if (os.startsWith("Windows")) {
116                    homeDir = homeDir.replace('\\', '/');
117                    homeDir = homeDir.replaceAll(" ", "%20");
118                }
119            }
120            
121            return homeDir;
122        }
123    
124        public void setHomeDir(String homeDir) {
125            this.homeDir = homeDir;
126        }
127    
128        public String getRepositoryDir() {
129            if (repositoryDir == null) {
130                if (System.getProperty("localRepository") != null) {
131                    repositoryDir = System.getProperty("localRepository");
132                } else {
133                    repositoryDir = getHomeDir() + "/.m2/repository";
134                }
135            }
136            return repositoryDir;
137        }
138    
139        public void setRepositoryDir(String repositoryDir) {
140            this.repositoryDir = repositoryDir;
141        }
142    
143        public String getVersion() {
144            if (version == null) {
145                version = createVersion();
146            }
147            return version;
148        }
149    
150        public void setVersion(String version) {
151            this.version = version;
152        }
153    
154        public String getFile() {
155            if (file == null) {
156                file = createFile();
157            }
158            return file;
159        }
160    
161        public void setFile(String file) {
162            this.file = file;
163        }
164    
165        public String getType() {
166            return type;
167        }
168    
169        public void setType(String type) {
170            this.type = type;
171        }
172    
173        // Implementation methods
174        //-------------------------------------------------------------------------
175        protected abstract void doDeploy() throws Exception;
176    
177        protected String createFile() {
178            String group = getGroupId();
179            String artifact = getArtifactId();
180            String v = getVersion();
181            if (v == null) {
182                throw new IllegalArgumentException(
183                        "You must specify a version property as it could not be deduced for "
184                                + getGroupId() + ":" + getArtifactId());
185            }
186            group = group.replace('.', '/');
187            return getFilePrefix() + getRepositoryDir() + "/" + group + "/" + artifact + "/" + v + "/" + artifact + "-" + v + type;
188        }
189    
190    
191        protected String createVersion() {
192            String group = getGroupId();
193            String artifact = getArtifactId();
194            String key = group + "/" + artifact + "/version";
195    
196            // now lets load all of the maven dependencies and look for this version
197            try {
198                Enumeration iter = Thread.currentThread().getContextClassLoader().getResources("META-INF/maven/dependencies.properties");
199                while (iter.hasMoreElements()) {
200                    URL url = (URL) iter.nextElement();
201    
202                    LOG.debug("looking into properties file: " + url + " with key: " + key);
203                    Properties properties = new Properties();
204                    InputStream in = url.openStream();
205                    properties.load(in);
206                    in.close();
207                    String answer = properties.getProperty(key);
208                    if (answer != null) {
209                        answer = answer.trim();
210                        LOG.debug("Found version: " + answer);
211                        return answer;
212                    }
213                }
214            } catch (IOException e) {
215                LOG.error("Failed: " + e, e);
216            }
217            return null;
218        }
219    
220        protected String getFilePrefix() {
221            String filePrefix = "file://";
222            String os = System.getProperty("os.name");
223            if (os.startsWith("Windows")) {
224                filePrefix = "file:///";
225            }
226    
227            return isFileUrlFormat() ? filePrefix : "";
228        }
229    
230        protected boolean isFileUrlFormat() {
231            return true;
232        }
233    }