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 propsFile = new File(dirName + "/" + fileName);
88                  File libDir = new File(propsFile.getParent() + "/lib");
89                  File projectDir = new File(propsFile.getParent() + "/../..");
90  
91                  Properties props = new Properties();
92  
93                  props.load(new FileInputStream(propsFile));
94  
95                  List<String> dependencyJars = ListUtil.toList(StringUtil.split(
96                      props.getProperty("portal.dependency.jars")));
97  
98                  if (svn) {
99                      List<String> jars = new ArrayList<String>(dependencyJars);
100 
101                     jars.add("commons-logging.jar");
102                     jars.add("log4j.jar");
103                     jars.add("util-bridges.jar");
104                     jars.add("util-java.jar");
105                     jars.add("util-taglib.jar");
106 
107                     jars = ListUtil.sort(jars);
108 
109                     updateLibIgnores(
110                         libDir, jars.toArray(new String[jars.size()]));
111                 }
112 
113                 if (eclipse) {
114                     updateEclipseFiles(libDir, projectDir, dependencyJars);
115                 }
116             }
117         }
118         catch (Exception e) {
119             e.printStackTrace();
120         }
121     }
122 
123     public void updateEclipseFiles(
124             File libDir, File projectDir, List<String> dependencyJars)
125         throws Exception {
126 
127         String projectDirName = projectDir.getCanonicalPath();
128         String projectName = StringUtil.extractLast(
129             projectDirName, File.separator);
130 
131         // .project
132 
133         StringBuilder sb = new StringBuilder();
134 
135         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
136         sb.append("<projectDescription>\n");
137         sb.append("\t<name>" + projectName + "</name>\n");
138         sb.append("\t<comment></comment>\n");
139         sb.append("\t<projects></projects>\n");
140         sb.append("\t<buildSpec>\n");
141         sb.append("\t\t<buildCommand>\n");
142         sb.append("\t\t\t<name>org.eclipse.jdt.core.javabuilder</name>\n");
143         sb.append("\t\t\t<arguments></arguments>\n");
144         sb.append("\t\t</buildCommand>\n");
145         sb.append("\t</buildSpec>\n");
146         sb.append("\t<natures>\n");
147         sb.append("\t\t<nature>org.eclipse.jdt.core.javanature</nature>\n");
148         sb.append("\t</natures>\n");
149         sb.append("</projectDescription>");
150 
151         File projectFile = new File(projectDirName + "/.project");
152 
153         System.out.println("Updating " + projectFile);
154 
155         FileUtil.write(projectFile, sb.toString());
156 
157         // .classpath
158 
159         List<String> portalJars = new ArrayList<String>(dependencyJars);
160 
161         portalJars.add("annotations.jar");
162         portalJars.add("commons-logging.jar");
163         portalJars.add("log4j.jar");
164 
165         portalJars = ListUtil.sort(portalJars);
166 
167         String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar"));
168 
169         List<String> customJars = null;
170 
171         if (customJarsArray != null) {
172             customJars = ListUtil.toList(customJarsArray);
173 
174             customJars = ListUtil.sort(customJars);
175 
176             for (String jar : portalJars) {
177                 customJars.remove(jar);
178             }
179 
180             customJars.remove(projectName + "-service.jar");
181             customJars.remove("util-bridges.jar");
182             customJars.remove("util-java.jar");
183             customJars.remove("util-taglib.jar");
184         }
185         else {
186             customJars = new ArrayList<String>();
187         }
188 
189         sb = new StringBuilder();
190 
191         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
192         sb.append("<classpath>\n");
193 
194         if (FileUtil.exists(projectDirName + "/docroot/WEB-INF/service")) {
195             sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
196             sb.append("kind=\"src\" path=\"docroot/WEB-INF/service\" />\n");
197         }
198 
199         sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
200         sb.append("kind=\"src\" path=\"docroot/WEB-INF/src\" />\n");
201         sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n");
202         sb.append("\t<classpathentry kind=\"con\" ");
203         sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n");
204 
205         _addClasspathEntry(sb, "/portal/lib/development/activation.jar");
206         _addClasspathEntry(sb, "/portal/lib/development/jsp-api.jar");
207         _addClasspathEntry(sb, "/portal/lib/development/mail.jar");
208         _addClasspathEntry(sb, "/portal/lib/development/servlet-api.jar");
209         _addClasspathEntry(sb, "/portal/lib/global/container.jar");
210         _addClasspathEntry(sb, "/portal/lib/global/portlet-container.jar");
211         _addClasspathEntry(sb, "/portal/lib/global/portlet.jar");
212 
213         for (String jar : portalJars) {
214             _addClasspathEntry(sb, "/portal/lib/portal/" + jar);
215         }
216 
217         _addClasspathEntry(sb, "/portal/portal-kernel/portal-kernel.jar");
218         _addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
219         _addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
220         _addClasspathEntry(sb, "/portal/util-java/util-java.jar");
221         _addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
222 
223         for (String jar : customJars) {
224             _addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
225         }
226 
227         sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
228         sb.append("</classpath>");
229 
230         File classpathFile = new File(projectDirName + "/.classpath");
231 
232         System.out.println("Updating " + classpathFile);
233 
234         FileUtil.write(classpathFile, sb.toString());
235 
236         // SVN
237 
238         if (_svn) {
239             String projectFileName = "\"" + projectFile + "\"";
240 
241             try {
242                 _exec(_SVN_INFO + projectFileName);
243             }
244             catch (Exception e) {
245                 _exec(_SVN_ADD + projectFileName);
246             }
247 
248             String classpathFileName = "\"" + classpathFile + "\"";
249 
250             try {
251                 _exec(_SVN_INFO + classpathFileName);
252             }
253             catch (Exception e) {
254                 _exec(_SVN_ADD + classpathFileName);
255             }
256 
257             _exec(_SVN_SET_IGNORES + "bin \"" + projectDirName + "\"");
258         }
259     }
260 
261     public void updateLibIgnores(File libDir, String[] jars) throws Exception {
262         if (!_isSVNDir(libDir)) {
263             return;
264         }
265 
266         File tempFile = null;
267 
268         try {
269             String libDirName = "\"" + libDir.getCanonicalPath() + "\"";
270 
271             String[] oldIgnores = _exec(_SVN_GET_IGNORES + libDirName);
272 
273             Arrays.sort(oldIgnores);
274 
275             if (Arrays.equals(oldIgnores, jars)) {
276                 return;
277             }
278 
279             tempFile = File.createTempFile("svn-ignores-", null, null);
280 
281             _exec(_SVN_DEL_IGNORES + libDirName);
282 
283             StringBuilder sb = new StringBuilder();
284 
285             for (String jar : jars) {
286                 sb.append(jar + "\n");
287             }
288 
289             FileUtil.write(tempFile, sb.toString());
290 
291             _exec(
292                 _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
293                     "\" " + libDirName);
294 
295             String[] newIgnores = _exec(_SVN_GET_IGNORES + libDirName);
296 
297             if (newIgnores.length > 0) {
298                 Arrays.sort(newIgnores);
299             }
300         }
301         finally {
302             if (tempFile != null) {
303                 tempFile.delete();
304             }
305         }
306     }
307 
308     private void _addClasspathEntry(StringBuilder sb, String jar)
309         throws Exception {
310 
311         sb.append("\t<classpathentry kind=\"lib\" path=\"");
312         sb.append(jar);
313         sb.append("\" />\n");
314     }
315 
316     private String[] _exec(String cmd) throws Exception {
317         Process process = Runtime.getRuntime().exec(cmd);
318 
319         String[] stdout = _getExecOutput(process.getInputStream());
320         String[] stderr = _getExecOutput(process.getErrorStream());
321 
322         if (stderr.length > 0) {
323             StringBuilder sb = new StringBuilder();
324 
325             sb.append("Received errors in executing '" + cmd + "'\n");
326 
327             for (String err : stderr) {
328                 sb.append("\t" + err + "\n");
329             }
330 
331             throw new Exception(sb.toString());
332         }
333 
334         return stdout;
335     }
336 
337     private String[] _getExecOutput(InputStream is) throws IOException {
338         List<String> list = new ArrayList<String>();
339 
340         BufferedReader br = null;
341 
342         try {
343             br = new BufferedReader(new InputStreamReader(is));
344 
345             String line = br.readLine();
346 
347             while (line != null) {
348                 line = line.trim();
349 
350                 if (Validator.isNotNull(line)) {
351                     list.add(line);
352                 }
353 
354                 line = br.readLine();
355             }
356         }
357         finally {
358             if (br != null) {
359                 try {
360                     br.close();
361                 }
362                 catch (Exception e) {
363                 }
364             }
365         }
366 
367         return list.toArray(new String[] {});
368     }
369 
370     private boolean _isSVNDir(File libDir) {
371         if (!libDir.exists()) {
372             return false;
373         }
374 
375         try {
376             _exec(_SVN_INFO + "\"" + libDir + "\"");
377         }
378         catch (Exception e) {
379             return false;
380         }
381 
382         return true;
383     }
384 
385     private static final String _SVN_ADD = "svn add ";
386 
387     private static final String _SVN_DEL_IGNORES = "svn propdel svn:ignore ";
388 
389     private static final String _SVN_GET_IGNORES = "svn propget svn:ignore ";
390 
391     private static final String _SVN_INFO = "svn info ";
392 
393     private static final String _SVN_SET_IGNORES = "svn propset svn:ignore ";
394 
395     private boolean _svn;
396 
397 }