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