1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.ListUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.InitUtil;
33  
34  import java.io.File;
35  import java.io.FileInputStream;
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.io.InputStreamReader;
39  
40  import java.util.ArrayList;
41  import java.util.Arrays;
42  import java.util.List;
43  import java.util.Properties;
44  
45  import org.apache.oro.io.GlobFilenameFilter;
46  import org.apache.tools.ant.DirectoryScanner;
47  
48  /**
49   * <a href="PluginsEnvironmentBuilder.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Alexander Chow
52   * @author Brian Wing Shun Chan
53   */
54  public class PluginsEnvironmentBuilder {
55  
56      public static void main(String[] args) throws Exception {
57          InitUtil.initWithSpring();
58  
59          File dir = new File(System.getProperty("plugins.env.dir"));
60          boolean svn = GetterUtil.getBoolean(
61              System.getProperty("plugins.env.svn"));
62          boolean eclipse = GetterUtil.getBoolean(
63              System.getProperty("plugins.env.eclipse"));
64  
65          new PluginsEnvironmentBuilder(dir, svn, eclipse);
66      }
67  
68      public PluginsEnvironmentBuilder(File dir, boolean svn, boolean eclipse) {
69          try {
70              _svn = svn;
71  
72              DirectoryScanner ds = new DirectoryScanner();
73  
74              ds.setBasedir(dir);
75              ds.setIncludes(
76                  new String[] {
77                      "**\\liferay-plugin-package.properties",
78                  });
79  
80              ds.scan();
81  
82              String dirName = dir.getCanonicalPath();
83  
84              String[] fileNames = ds.getIncludedFiles();
85  
86              for (String fileName : fileNames) {
87                  File propertiesFile = new File(dirName + "/" + fileName);
88                  File libDir = new File(propertiesFile.getParent() + "/lib");
89                  File projectDir = new File(
90                      propertiesFile.getParent() + "/../..");
91  
92                  Properties properties = new Properties();
93  
94                  properties.load(new FileInputStream(propertiesFile));
95  
96                  List<String> dependencyJars = ListUtil.toList(StringUtil.split(
97                      properties.getProperty(
98                          "portal-dependency-jars",
99                          properties.getProperty("portal.dependency.jars"))));
100 
101                 if (svn) {
102                     List<String> jars = new ArrayList<String>(dependencyJars);
103 
104                     jars.add("commons-logging.jar");
105                     jars.add("log4j.jar");
106                     jars.add("util-bridges.jar");
107                     jars.add("util-java.jar");
108                     jars.add("util-taglib.jar");
109 
110                     jars = ListUtil.sort(jars);
111 
112                     updateLibIgnores(
113                         libDir, jars.toArray(new String[jars.size()]));
114                 }
115 
116                 if (eclipse) {
117                     updateEclipseFiles(libDir, projectDir, dependencyJars);
118                 }
119             }
120         }
121         catch (Exception e) {
122             e.printStackTrace();
123         }
124     }
125 
126     public void updateEclipseFiles(
127             File libDir, File projectDir, List<String> dependencyJars)
128         throws Exception {
129 
130         String libDirPath = libDir.getPath();
131 
132         libDirPath = StringUtil.replace(
133             libDirPath, StringPool.BACK_SLASH, StringPool.SLASH);
134 
135         String projectDirName = projectDir.getCanonicalPath();
136         String projectName = StringUtil.extractLast(
137             projectDirName, File.separator);
138 
139         // .project
140 
141         StringBuilder sb = new StringBuilder();
142 
143         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
144         sb.append("<projectDescription>\n");
145         sb.append("\t<name>" + projectName + "</name>\n");
146         sb.append("\t<comment></comment>\n");
147         sb.append("\t<projects></projects>\n");
148         sb.append("\t<buildSpec>\n");
149         sb.append("\t\t<buildCommand>\n");
150         sb.append("\t\t\t<name>org.eclipse.jdt.core.javabuilder</name>\n");
151         sb.append("\t\t\t<arguments></arguments>\n");
152         sb.append("\t\t</buildCommand>\n");
153         sb.append("\t</buildSpec>\n");
154         sb.append("\t<natures>\n");
155         sb.append("\t\t<nature>org.eclipse.jdt.core.javanature</nature>\n");
156         sb.append("\t</natures>\n");
157         sb.append("</projectDescription>");
158 
159         File projectFile = new File(projectDirName + "/.project");
160 
161         System.out.println("Updating " + projectFile);
162 
163         FileUtil.write(projectFile, sb.toString());
164 
165         // .classpath
166 
167         List<String> portalJars = new ArrayList<String>(dependencyJars);
168 
169         portalJars.add("commons-logging.jar");
170         portalJars.add("log4j.jar");
171 
172         portalJars = ListUtil.sort(portalJars);
173 
174         String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar"));
175 
176         List<String> customJars = null;
177 
178         if (customJarsArray != null) {
179             customJars = ListUtil.toList(customJarsArray);
180 
181             customJars = ListUtil.sort(customJars);
182 
183             for (String jar : portalJars) {
184                 customJars.remove(jar);
185             }
186 
187             customJars.remove(projectName + "-service.jar");
188             customJars.remove("util-bridges.jar");
189             customJars.remove("util-java.jar");
190             customJars.remove("util-taglib.jar");
191         }
192         else {
193             customJars = new ArrayList<String>();
194         }
195 
196         sb = new StringBuilder();
197 
198         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
199         sb.append("<classpath>\n");
200 
201         if (FileUtil.exists(projectDirName + "/docroot/WEB-INF/service")) {
202             sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
203             sb.append("kind=\"src\" path=\"docroot/WEB-INF/service\" />\n");
204         }
205 
206         sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
207         sb.append("kind=\"src\" path=\"docroot/WEB-INF/src\" />\n");
208         sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n");
209         sb.append("\t<classpathentry kind=\"con\" ");
210         sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n");
211 
212         if (FileUtil.exists(projectDirName + "/docroot/WEB-INF/test")) {
213             sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
214             sb.append("kind=\"src\" path=\"docroot/WEB-INF/test\" />\n");
215         }
216 
217         _addClasspathEntry(sb, "/portal/lib/development/activation.jar");
218         _addClasspathEntry(sb, "/portal/lib/development/jsp-api.jar");
219         _addClasspathEntry(sb, "/portal/lib/development/mail.jar");
220         _addClasspathEntry(sb, "/portal/lib/development/servlet-api.jar");
221         _addClasspathEntry(sb, "/portal/lib/global/annotations.jar");
222         _addClasspathEntry(sb, "/portal/lib/global/portlet.jar");
223 
224         for (String jar : portalJars) {
225             _addClasspathEntry(sb, "/portal/lib/portal/" + jar);
226         }
227 
228         _addClasspathEntry(sb, "/portal/portal-kernel/portal-kernel.jar");
229         _addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
230         _addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
231         _addClasspathEntry(sb, "/portal/util-java/util-java.jar");
232         _addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
233 
234         for (String jar : customJars) {
235             if (libDirPath.contains("/tmp/WEB-INF/lib")) {
236                 _addClasspathEntry(sb, "tmp/WEB-INF/lib/" + jar);
237             }
238             else {
239                 _addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
240             }
241         }
242 
243         sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
244         sb.append("</classpath>");
245 
246         File classpathFile = new File(projectDirName + "/.classpath");
247 
248         System.out.println("Updating " + classpathFile);
249 
250         FileUtil.write(classpathFile, sb.toString());
251 
252         // SVN
253 
254         if (_svn) {
255             String projectFileName = "\"" + projectFile + "\"";
256 
257             try {
258                 _exec(_SVN_INFO + projectFileName);
259             }
260             catch (Exception e) {
261                 _exec(_SVN_ADD + projectFileName);
262             }
263 
264             String classpathFileName = "\"" + classpathFile + "\"";
265 
266             try {
267                 _exec(_SVN_INFO + classpathFileName);
268             }
269             catch (Exception e) {
270                 _exec(_SVN_ADD + classpathFileName);
271             }
272 
273             File tempFile = File.createTempFile("svn-ignores-", null, null);
274 
275             try {
276                 FileUtil.write(tempFile, "bin\ntmp");
277 
278                 _exec(
279                     _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
280                         "\" \"" + projectDirName + "\"");
281             }
282             finally {
283                 FileUtil.delete(tempFile);
284             }
285         }
286     }
287 
288     public void updateLibIgnores(File libDir, String[] jars) throws Exception {
289         if (!_isSVNDir(libDir)) {
290             return;
291         }
292 
293         File tempFile = null;
294 
295         try {
296             String libDirName = "\"" + libDir.getCanonicalPath() + "\"";
297 
298             String[] oldIgnores = _exec(_SVN_GET_IGNORES + libDirName);
299 
300             Arrays.sort(oldIgnores);
301 
302             if (Arrays.equals(oldIgnores, jars)) {
303                 return;
304             }
305 
306             tempFile = File.createTempFile("svn-ignores-", null, null);
307 
308             _exec(_SVN_DEL_IGNORES + libDirName);
309 
310             StringBuilder sb = new StringBuilder();
311 
312             for (String jar : jars) {
313                 sb.append(jar + "\n");
314             }
315 
316             FileUtil.write(tempFile, sb.toString());
317 
318             _exec(
319                 _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
320                     "\" \"" + libDirName + "\"");
321 
322             String[] newIgnores = _exec(
323                 _SVN_GET_IGNORES + "\"" + libDirName + "\"");
324 
325             if (newIgnores.length > 0) {
326                 Arrays.sort(newIgnores);
327             }
328         }
329         finally {
330             if (tempFile != null) {
331                 FileUtil.delete(tempFile);
332             }
333         }
334     }
335 
336     private void _addClasspathEntry(StringBuilder sb, String jar)
337         throws Exception {
338 
339         sb.append("\t<classpathentry kind=\"lib\" path=\"");
340         sb.append(jar);
341         sb.append("\" />\n");
342     }
343 
344     private String[] _exec(String cmd) throws Exception {
345         Process process = Runtime.getRuntime().exec(cmd);
346 
347         String[] stdout = _getExecOutput(process.getInputStream());
348         String[] stderr = _getExecOutput(process.getErrorStream());
349 
350         if (stderr.length > 0) {
351             StringBuilder sb = new StringBuilder();
352 
353             sb.append("Received errors in executing '" + cmd + "'\n");
354 
355             for (String err : stderr) {
356                 sb.append("\t" + err + "\n");
357             }
358 
359             throw new Exception(sb.toString());
360         }
361 
362         return stdout;
363     }
364 
365     private String[] _getExecOutput(InputStream is) throws IOException {
366         List<String> list = new ArrayList<String>();
367 
368         UnsyncBufferedReader unsyncBufferedReader = null;
369 
370         try {
371             unsyncBufferedReader = new UnsyncBufferedReader(
372                 new InputStreamReader(is));
373 
374             String line = unsyncBufferedReader.readLine();
375 
376             while (line != null) {
377                 line = line.trim();
378 
379                 if (Validator.isNotNull(line)) {
380                     list.add(line);
381                 }
382 
383                 line = unsyncBufferedReader.readLine();
384             }
385         }
386         finally {
387             if (unsyncBufferedReader != null) {
388                 try {
389                     unsyncBufferedReader.close();
390                 }
391                 catch (Exception e) {
392                 }
393             }
394         }
395 
396         return list.toArray(new String[] {});
397     }
398 
399     private boolean _isSVNDir(File libDir) {
400         if (!libDir.exists()) {
401             return false;
402         }
403 
404         try {
405             _exec(_SVN_INFO + "\"" + libDir + "\"");
406         }
407         catch (Exception e) {
408             return false;
409         }
410 
411         return true;
412     }
413 
414     private static final String _SVN_ADD = "svn add ";
415 
416     private static final String _SVN_DEL_IGNORES = "svn propdel svn:ignore ";
417 
418     private static final String _SVN_GET_IGNORES = "svn propget svn:ignore ";
419 
420     private static final String _SVN_INFO = "svn info ";
421 
422     private static final String _SVN_SET_IGNORES = "svn propset svn:ignore ";
423 
424     private boolean _svn;
425 
426 }