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("commons-logging.jar");
165         portalJars.add("log4j.jar");
166 
167         portalJars = ListUtil.sort(portalJars);
168 
169         String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar"));
170 
171         List<String> customJars = null;
172 
173         if (customJarsArray != null) {
174             customJars = ListUtil.toList(customJarsArray);
175 
176             customJars = ListUtil.sort(customJars);
177 
178             for (String jar : portalJars) {
179                 customJars.remove(jar);
180             }
181 
182             customJars.remove(projectName + "-service.jar");
183             customJars.remove("util-bridges.jar");
184             customJars.remove("util-java.jar");
185             customJars.remove("util-taglib.jar");
186         }
187         else {
188             customJars = new ArrayList<String>();
189         }
190 
191         sb = new StringBuilder();
192 
193         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
194         sb.append("<classpath>\n");
195 
196         if (FileUtil.exists(projectDirName + "/docroot/WEB-INF/service")) {
197             sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
198             sb.append("kind=\"src\" path=\"docroot/WEB-INF/service\" />\n");
199         }
200 
201         sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
202         sb.append("kind=\"src\" path=\"docroot/WEB-INF/src\" />\n");
203         sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n");
204         sb.append("\t<classpathentry kind=\"con\" ");
205         sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n");
206 
207         if (FileUtil.exists(projectDirName + "/docroot/WEB-INF/test")) {
208             sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
209             sb.append("kind=\"src\" path=\"docroot/WEB-INF/test\" />\n");
210         }
211 
212         _addClasspathEntry(sb, "/portal/lib/development/activation.jar");
213         _addClasspathEntry(sb, "/portal/lib/development/jsp-api.jar");
214         _addClasspathEntry(sb, "/portal/lib/development/mail.jar");
215         _addClasspathEntry(sb, "/portal/lib/development/servlet-api.jar");
216         _addClasspathEntry(sb, "/portal/lib/global/annotations.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             File tempFile = File.createTempFile("svn-ignores-", null, null);
266 
267             try {
268                 FileUtil.write(tempFile, "bin\ntmp");
269 
270                 _exec(
271                     _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
272                         "\" \"" + projectDirName + "\"");
273             }
274             finally {
275                 FileUtil.delete(tempFile);
276             }
277         }
278     }
279 
280     public void updateLibIgnores(File libDir, String[] jars) throws Exception {
281         if (!_isSVNDir(libDir)) {
282             return;
283         }
284 
285         File tempFile = null;
286 
287         try {
288             String libDirName = "\"" + libDir.getCanonicalPath() + "\"";
289 
290             String[] oldIgnores = _exec(_SVN_GET_IGNORES + libDirName);
291 
292             Arrays.sort(oldIgnores);
293 
294             if (Arrays.equals(oldIgnores, jars)) {
295                 return;
296             }
297 
298             tempFile = File.createTempFile("svn-ignores-", null, null);
299 
300             _exec(_SVN_DEL_IGNORES + libDirName);
301 
302             StringBuilder sb = new StringBuilder();
303 
304             for (String jar : jars) {
305                 sb.append(jar + "\n");
306             }
307 
308             FileUtil.write(tempFile, sb.toString());
309 
310             _exec(
311                 _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
312                     "\" \"" + libDirName + "\"");
313 
314             String[] newIgnores = _exec(
315                 _SVN_GET_IGNORES + "\"" + libDirName + "\"");
316 
317             if (newIgnores.length > 0) {
318                 Arrays.sort(newIgnores);
319             }
320         }
321         finally {
322             if (tempFile != null) {
323                 FileUtil.delete(tempFile);
324             }
325         }
326     }
327 
328     private void _addClasspathEntry(StringBuilder sb, String jar)
329         throws Exception {
330 
331         sb.append("\t<classpathentry kind=\"lib\" path=\"");
332         sb.append(jar);
333         sb.append("\" />\n");
334     }
335 
336     private String[] _exec(String cmd) throws Exception {
337         Process process = Runtime.getRuntime().exec(cmd);
338 
339         String[] stdout = _getExecOutput(process.getInputStream());
340         String[] stderr = _getExecOutput(process.getErrorStream());
341 
342         if (stderr.length > 0) {
343             StringBuilder sb = new StringBuilder();
344 
345             sb.append("Received errors in executing '" + cmd + "'\n");
346 
347             for (String err : stderr) {
348                 sb.append("\t" + err + "\n");
349             }
350 
351             throw new Exception(sb.toString());
352         }
353 
354         return stdout;
355     }
356 
357     private String[] _getExecOutput(InputStream is) throws IOException {
358         List<String> list = new ArrayList<String>();
359 
360         BufferedReader br = null;
361 
362         try {
363             br = new BufferedReader(new InputStreamReader(is));
364 
365             String line = br.readLine();
366 
367             while (line != null) {
368                 line = line.trim();
369 
370                 if (Validator.isNotNull(line)) {
371                     list.add(line);
372                 }
373 
374                 line = br.readLine();
375             }
376         }
377         finally {
378             if (br != null) {
379                 try {
380                     br.close();
381                 }
382                 catch (Exception e) {
383                 }
384             }
385         }
386 
387         return list.toArray(new String[] {});
388     }
389 
390     private boolean _isSVNDir(File libDir) {
391         if (!libDir.exists()) {
392             return false;
393         }
394 
395         try {
396             _exec(_SVN_INFO + "\"" + libDir + "\"");
397         }
398         catch (Exception e) {
399             return false;
400         }
401 
402         return true;
403     }
404 
405     private static final String _SVN_ADD = "svn add ";
406 
407     private static final String _SVN_DEL_IGNORES = "svn propdel svn:ignore ";
408 
409     private static final String _SVN_GET_IGNORES = "svn propget svn:ignore ";
410 
411     private static final String _SVN_INFO = "svn info ";
412 
413     private static final String _SVN_SET_IGNORES = "svn propset svn:ignore ";
414 
415     private boolean _svn;
416 
417 }