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/portlet.jar");
218 
219         for (String jar : portalJars) {
220             _addClasspathEntry(sb, "/portal/lib/portal/" + jar);
221         }
222 
223         _addClasspathEntry(sb, "/portal/portal-kernel/portal-kernel.jar");
224         _addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
225         _addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
226         _addClasspathEntry(sb, "/portal/util-java/util-java.jar");
227         _addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
228 
229         for (String jar : customJars) {
230             _addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
231         }
232 
233         sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
234         sb.append("</classpath>");
235 
236         File classpathFile = new File(projectDirName + "/.classpath");
237 
238         System.out.println("Updating " + classpathFile);
239 
240         FileUtil.write(classpathFile, sb.toString());
241 
242         // SVN
243 
244         if (_svn) {
245             String projectFileName = "\"" + projectFile + "\"";
246 
247             try {
248                 _exec(_SVN_INFO + projectFileName);
249             }
250             catch (Exception e) {
251                 _exec(_SVN_ADD + projectFileName);
252             }
253 
254             String classpathFileName = "\"" + classpathFile + "\"";
255 
256             try {
257                 _exec(_SVN_INFO + classpathFileName);
258             }
259             catch (Exception e) {
260                 _exec(_SVN_ADD + classpathFileName);
261             }
262 
263             File tempFile = File.createTempFile("svn-ignores-", null, null);
264 
265             try {
266                 FileUtil.write(tempFile, "bin\ntmp");
267 
268                 _exec(
269                     _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
270                         "\" \"" + projectDirName + "\"");
271             }
272             finally {
273                 FileUtil.delete(tempFile);
274             }
275         }
276     }
277 
278     public void updateLibIgnores(File libDir, String[] jars) throws Exception {
279         if (!_isSVNDir(libDir)) {
280             return;
281         }
282 
283         File tempFile = null;
284 
285         try {
286             String libDirName = "\"" + libDir.getCanonicalPath() + "\"";
287 
288             String[] oldIgnores = _exec(_SVN_GET_IGNORES + libDirName);
289 
290             Arrays.sort(oldIgnores);
291 
292             if (Arrays.equals(oldIgnores, jars)) {
293                 return;
294             }
295 
296             tempFile = File.createTempFile("svn-ignores-", null, null);
297 
298             _exec(_SVN_DEL_IGNORES + libDirName);
299 
300             StringBuilder sb = new StringBuilder();
301 
302             for (String jar : jars) {
303                 sb.append(jar + "\n");
304             }
305 
306             FileUtil.write(tempFile, sb.toString());
307 
308             _exec(
309                 _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
310                     "\" \"" + libDirName + "\"");
311 
312             String[] newIgnores = _exec(
313                 _SVN_GET_IGNORES + "\"" + libDirName + "\"");
314 
315             if (newIgnores.length > 0) {
316                 Arrays.sort(newIgnores);
317             }
318         }
319         finally {
320             if (tempFile != null) {
321                 FileUtil.delete(tempFile);
322             }
323         }
324     }
325 
326     private void _addClasspathEntry(StringBuilder sb, String jar)
327         throws Exception {
328 
329         sb.append("\t<classpathentry kind=\"lib\" path=\"");
330         sb.append(jar);
331         sb.append("\" />\n");
332     }
333 
334     private String[] _exec(String cmd) throws Exception {
335         Process process = Runtime.getRuntime().exec(cmd);
336 
337         String[] stdout = _getExecOutput(process.getInputStream());
338         String[] stderr = _getExecOutput(process.getErrorStream());
339 
340         if (stderr.length > 0) {
341             StringBuilder sb = new StringBuilder();
342 
343             sb.append("Received errors in executing '" + cmd + "'\n");
344 
345             for (String err : stderr) {
346                 sb.append("\t" + err + "\n");
347             }
348 
349             throw new Exception(sb.toString());
350         }
351 
352         return stdout;
353     }
354 
355     private String[] _getExecOutput(InputStream is) throws IOException {
356         List<String> list = new ArrayList<String>();
357 
358         BufferedReader br = null;
359 
360         try {
361             br = new BufferedReader(new InputStreamReader(is));
362 
363             String line = br.readLine();
364 
365             while (line != null) {
366                 line = line.trim();
367 
368                 if (Validator.isNotNull(line)) {
369                     list.add(line);
370                 }
371 
372                 line = br.readLine();
373             }
374         }
375         finally {
376             if (br != null) {
377                 try {
378                     br.close();
379                 }
380                 catch (Exception e) {
381                 }
382             }
383         }
384 
385         return list.toArray(new String[] {});
386     }
387 
388     private boolean _isSVNDir(File libDir) {
389         if (!libDir.exists()) {
390             return false;
391         }
392 
393         try {
394             _exec(_SVN_INFO + "\"" + libDir + "\"");
395         }
396         catch (Exception e) {
397             return false;
398         }
399 
400         return true;
401     }
402 
403     private static final String _SVN_ADD = "svn add ";
404 
405     private static final String _SVN_DEL_IGNORES = "svn propdel svn:ignore ";
406 
407     private static final String _SVN_GET_IGNORES = "svn propget svn:ignore ";
408 
409     private static final String _SVN_INFO = "svn info ";
410 
411     private static final String _SVN_SET_IGNORES = "svn propset svn:ignore ";
412 
413     private boolean _svn;
414 
415 }