1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.InitUtil;
31  
32  import java.io.BufferedReader;
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.io.InputStreamReader;
38  
39  import java.util.ArrayList;
40  import java.util.Arrays;
41  import java.util.List;
42  import java.util.Properties;
43  
44  import org.apache.oro.io.GlobFilenameFilter;
45  import org.apache.tools.ant.DirectoryScanner;
46  
47  /**
48   * <a href="PluginsEnvironmentBuilder.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Alexander Chow
51   * @author Brian Wing Shun Chan
52   *
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 projectDirName = projectDir.getCanonicalPath();
131         String projectName = StringUtil.extractLast(
132             projectDirName, File.separator);
133 
134         // .project
135 
136         StringBuilder sb = new StringBuilder();
137 
138         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
139         sb.append("<projectDescription>\n");
140         sb.append("\t<name>" + projectName + "</name>\n");
141         sb.append("\t<comment></comment>\n");
142         sb.append("\t<projects></projects>\n");
143         sb.append("\t<buildSpec>\n");
144         sb.append("\t\t<buildCommand>\n");
145         sb.append("\t\t\t<name>org.eclipse.jdt.core.javabuilder</name>\n");
146         sb.append("\t\t\t<arguments></arguments>\n");
147         sb.append("\t\t</buildCommand>\n");
148         sb.append("\t</buildSpec>\n");
149         sb.append("\t<natures>\n");
150         sb.append("\t\t<nature>org.eclipse.jdt.core.javanature</nature>\n");
151         sb.append("\t</natures>\n");
152         sb.append("</projectDescription>");
153 
154         File projectFile = new File(projectDirName + "/.project");
155 
156         System.out.println("Updating " + projectFile);
157 
158         FileUtil.write(projectFile, sb.toString());
159 
160         // .classpath
161 
162         List<String> portalJars = new ArrayList<String>(dependencyJars);
163 
164         portalJars.add("annotations.jar");
165         portalJars.add("commons-logging.jar");
166         portalJars.add("log4j.jar");
167 
168         portalJars = ListUtil.sort(portalJars);
169 
170         String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar"));
171 
172         List<String> customJars = null;
173 
174         if (customJarsArray != null) {
175             customJars = ListUtil.toList(customJarsArray);
176 
177             customJars = ListUtil.sort(customJars);
178 
179             for (String jar : portalJars) {
180                 customJars.remove(jar);
181             }
182 
183             customJars.remove(projectName + "-service.jar");
184             customJars.remove("util-bridges.jar");
185             customJars.remove("util-java.jar");
186             customJars.remove("util-taglib.jar");
187         }
188         else {
189             customJars = new ArrayList<String>();
190         }
191 
192         sb = new StringBuilder();
193 
194         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
195         sb.append("<classpath>\n");
196 
197         if (FileUtil.exists(projectDirName + "/docroot/WEB-INF/service")) {
198             sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
199             sb.append("kind=\"src\" path=\"docroot/WEB-INF/service\" />\n");
200         }
201 
202         sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
203         sb.append("kind=\"src\" path=\"docroot/WEB-INF/src\" />\n");
204         sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n");
205         sb.append("\t<classpathentry kind=\"con\" ");
206         sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n");
207 
208         if (FileUtil.exists(projectDirName + "/docroot/WEB-INF/test")) {
209             sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
210             sb.append("kind=\"src\" path=\"docroot/WEB-INF/test\" />\n");
211         }
212 
213         _addClasspathEntry(sb, "/portal/lib/development/activation.jar");
214         _addClasspathEntry(sb, "/portal/lib/development/jsp-api.jar");
215         _addClasspathEntry(sb, "/portal/lib/development/mail.jar");
216         _addClasspathEntry(sb, "/portal/lib/development/servlet-api.jar");
217         _addClasspathEntry(sb, "/portal/lib/global/container.jar");
218         _addClasspathEntry(sb, "/portal/lib/global/portlet-container.jar");
219         _addClasspathEntry(sb, "/portal/lib/global/portlet.jar");
220 
221         for (String jar : portalJars) {
222             _addClasspathEntry(sb, "/portal/lib/portal/" + jar);
223         }
224 
225         _addClasspathEntry(sb, "/portal/portal-kernel/portal-kernel.jar");
226         _addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
227         _addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
228         _addClasspathEntry(sb, "/portal/util-java/util-java.jar");
229         _addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
230 
231         for (String jar : customJars) {
232             _addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
233         }
234 
235         sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
236         sb.append("</classpath>");
237 
238         File classpathFile = new File(projectDirName + "/.classpath");
239 
240         System.out.println("Updating " + classpathFile);
241 
242         FileUtil.write(classpathFile, sb.toString());
243 
244         // SVN
245 
246         if (_svn) {
247             String projectFileName = "\"" + projectFile + "\"";
248 
249             try {
250                 _exec(_SVN_INFO + projectFileName);
251             }
252             catch (Exception e) {
253                 _exec(_SVN_ADD + projectFileName);
254             }
255 
256             String classpathFileName = "\"" + classpathFile + "\"";
257 
258             try {
259                 _exec(_SVN_INFO + classpathFileName);
260             }
261             catch (Exception e) {
262                 _exec(_SVN_ADD + classpathFileName);
263             }
264 
265             _exec(_SVN_SET_IGNORES + "bin \"" + projectDirName + "\"");
266         }
267     }
268 
269     public void updateLibIgnores(File libDir, String[] jars) throws Exception {
270         if (!_isSVNDir(libDir)) {
271             return;
272         }
273 
274         File tempFile = null;
275 
276         try {
277             String libDirName = "\"" + libDir.getCanonicalPath() + "\"";
278 
279             String[] oldIgnores = _exec(_SVN_GET_IGNORES + libDirName);
280 
281             Arrays.sort(oldIgnores);
282 
283             if (Arrays.equals(oldIgnores, jars)) {
284                 return;
285             }
286 
287             tempFile = File.createTempFile("svn-ignores-", null, null);
288 
289             _exec(_SVN_DEL_IGNORES + libDirName);
290 
291             StringBuilder sb = new StringBuilder();
292 
293             for (String jar : jars) {
294                 sb.append(jar + "\n");
295             }
296 
297             FileUtil.write(tempFile, sb.toString());
298 
299             _exec(
300                 _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
301                     "\" " + libDirName);
302 
303             String[] newIgnores = _exec(_SVN_GET_IGNORES + libDirName);
304 
305             if (newIgnores.length > 0) {
306                 Arrays.sort(newIgnores);
307             }
308         }
309         finally {
310             if (tempFile != null) {
311                 tempFile.delete();
312             }
313         }
314     }
315 
316     private void _addClasspathEntry(StringBuilder sb, String jar)
317         throws Exception {
318 
319         sb.append("\t<classpathentry kind=\"lib\" path=\"");
320         sb.append(jar);
321         sb.append("\" />\n");
322     }
323 
324     private String[] _exec(String cmd) throws Exception {
325         Process process = Runtime.getRuntime().exec(cmd);
326 
327         String[] stdout = _getExecOutput(process.getInputStream());
328         String[] stderr = _getExecOutput(process.getErrorStream());
329 
330         if (stderr.length > 0) {
331             StringBuilder sb = new StringBuilder();
332 
333             sb.append("Received errors in executing '" + cmd + "'\n");
334 
335             for (String err : stderr) {
336                 sb.append("\t" + err + "\n");
337             }
338 
339             throw new Exception(sb.toString());
340         }
341 
342         return stdout;
343     }
344 
345     private String[] _getExecOutput(InputStream is) throws IOException {
346         List<String> list = new ArrayList<String>();
347 
348         BufferedReader br = null;
349 
350         try {
351             br = new BufferedReader(new InputStreamReader(is));
352 
353             String line = br.readLine();
354 
355             while (line != null) {
356                 line = line.trim();
357 
358                 if (Validator.isNotNull(line)) {
359                     list.add(line);
360                 }
361 
362                 line = br.readLine();
363             }
364         }
365         finally {
366             if (br != null) {
367                 try {
368                     br.close();
369                 }
370                 catch (Exception e) {
371                 }
372             }
373         }
374 
375         return list.toArray(new String[] {});
376     }
377 
378     private boolean _isSVNDir(File libDir) {
379         if (!libDir.exists()) {
380             return false;
381         }
382 
383         try {
384             _exec(_SVN_INFO + "\"" + libDir + "\"");
385         }
386         catch (Exception e) {
387             return false;
388         }
389 
390         return true;
391     }
392 
393     private static final String _SVN_ADD = "svn add ";
394 
395     private static final String _SVN_DEL_IGNORES = "svn propdel svn:ignore ";
396 
397     private static final String _SVN_GET_IGNORES = "svn propget svn:ignore ";
398 
399     private static final String _SVN_INFO = "svn info ";
400 
401     private static final String _SVN_SET_IGNORES = "svn propset svn:ignore ";
402 
403     private boolean _svn;
404 
405 }