1
14
15 package com.liferay.portal.tools;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18 import com.liferay.portal.kernel.util.FileUtil;
19 import com.liferay.portal.kernel.util.GetterUtil;
20 import com.liferay.portal.kernel.util.ListUtil;
21 import com.liferay.portal.kernel.util.StringBundler;
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.portal.kernel.util.StringUtil;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.util.InitUtil;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Properties;
37
38 import org.apache.oro.io.GlobFilenameFilter;
39 import org.apache.tools.ant.DirectoryScanner;
40
41
47 public class PluginsEnvironmentBuilder {
48
49 public static void main(String[] args) throws Exception {
50 InitUtil.initWithSpring();
51
52 File dir = new File(System.getProperty("plugins.env.dir"));
53 boolean svn = GetterUtil.getBoolean(
54 System.getProperty("plugins.env.svn"));
55 boolean eclipse = GetterUtil.getBoolean(
56 System.getProperty("plugins.env.eclipse"));
57
58 new PluginsEnvironmentBuilder(dir, svn, eclipse);
59 }
60
61 public PluginsEnvironmentBuilder(File dir, boolean svn, boolean eclipse) {
62 try {
63 _svn = svn;
64
65 DirectoryScanner ds = new DirectoryScanner();
66
67 ds.setBasedir(dir);
68 ds.setIncludes(
69 new String[] {
70 "**\\liferay-plugin-package.properties",
71 });
72
73 ds.scan();
74
75 String dirName = dir.getCanonicalPath();
76
77 String[] fileNames = ds.getIncludedFiles();
78
79 for (String fileName : fileNames) {
80 File propertiesFile = new File(dirName + "/" + fileName);
81 File libDir = new File(propertiesFile.getParent() + "/lib");
82 File projectDir = new File(
83 propertiesFile.getParent() + "/../..");
84
85 Properties properties = new Properties();
86
87 properties.load(new FileInputStream(propertiesFile));
88
89 String[] dependencyJars = StringUtil.split(
90 properties.getProperty(
91 "portal-dependency-jars",
92 properties.getProperty("portal.dependency.jars")));
93
94 if (svn) {
95 List<String> jars = ListUtil.toList(dependencyJars);
96
97 jars.add("commons-logging.jar");
98 jars.add("log4j.jar");
99 jars.add("util-bridges.jar");
100 jars.add("util-java.jar");
101 jars.add("util-taglib.jar");
102
103 jars = ListUtil.sort(jars);
104
105 updateLibIgnores(
106 libDir, jars.toArray(new String[jars.size()]));
107 }
108
109 if (eclipse) {
110 updateEclipseFiles(libDir, projectDir, dependencyJars);
111 }
112 }
113 }
114 catch (Exception e) {
115 e.printStackTrace();
116 }
117 }
118
119 public void updateEclipseFiles(
120 File libDir, File projectDir, String[] dependencyJars)
121 throws Exception {
122
123 String libDirPath = libDir.getPath();
124
125 libDirPath = StringUtil.replace(
126 libDirPath, StringPool.BACK_SLASH, StringPool.SLASH);
127
128 String projectDirName = projectDir.getCanonicalPath();
129 String projectName = StringUtil.extractLast(
130 projectDirName, File.separator);
131
132
134 StringBundler sb = new StringBundler(17);
135
136 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
137 sb.append("<projectDescription>\n");
138 sb.append("\t<name>");
139 sb.append(projectName);
140 sb.append("</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
162 List<String> portalJars = ListUtil.toList(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 StringBundler();
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/annotations.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/portlet.jar");
218
219 for (String jar : portalJars) {
220 _addClasspathEntry(sb, "/portal/lib/portal/" + jar);
221 }
222
223 _addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
224 _addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
225 _addClasspathEntry(sb, "/portal/util-java/util-java.jar");
226 _addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
227
228 for (String jar : customJars) {
229 if (libDirPath.contains("/tmp/WEB-INF/lib")) {
230 _addClasspathEntry(sb, "tmp/WEB-INF/lib/" + jar);
231 }
232 else {
233 _addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
234 }
235 }
236
237 sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
238 sb.append("</classpath>");
239
240 File classpathFile = new File(projectDirName + "/.classpath");
241
242 System.out.println("Updating " + classpathFile);
243
244 FileUtil.write(classpathFile, sb.toString());
245
246
248 if (_svn) {
249 String projectFileName = "\"" + projectFile + "\"";
250
251 try {
252 _exec(_SVN_INFO + projectFileName);
253 }
254 catch (Exception e) {
255 _exec(_SVN_ADD + projectFileName);
256 }
257
258 String classpathFileName = "\"" + classpathFile + "\"";
259
260 try {
261 _exec(_SVN_INFO + classpathFileName);
262 }
263 catch (Exception e) {
264 _exec(_SVN_ADD + classpathFileName);
265 }
266
267 File tempFile = File.createTempFile("svn-ignores-", null, null);
268
269 try {
270 FileUtil.write(tempFile, "bin\ntmp");
271
272 _exec(
273 _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
274 "\" \"" + projectDirName + "\"");
275 }
276 finally {
277 FileUtil.delete(tempFile);
278 }
279 }
280 }
281
282 public void updateLibIgnores(File libDir, String[] jars) throws Exception {
283 if (!_isSVNDir(libDir)) {
284 return;
285 }
286
287 File tempFile = null;
288
289 try {
290 String libDirName = "\"" + libDir.getCanonicalPath() + "\"";
291
292 String[] oldIgnores = _exec(_SVN_GET_IGNORES + libDirName);
293
294 Arrays.sort(oldIgnores);
295
296 if (Arrays.equals(oldIgnores, jars)) {
297 return;
298 }
299
300 tempFile = File.createTempFile("svn-ignores-", null, null);
301
302 _exec(_SVN_DEL_IGNORES + libDirName);
303
304 if (jars.length == 0) {
305 FileUtil.write(tempFile, StringPool.BLANK);
306 }
307 else {
308 StringBundler sb = new StringBundler(jars.length * 2);
309
310 for (String jar : jars) {
311 sb.append(jar);
312 sb.append("\n");
313 }
314
315 FileUtil.write(tempFile, sb.toString());
316 }
317
318 _exec(
319 _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
320 "\" \"" + libDirName + "\"");
321
322 String[] newIgnores = _exec(
323 _SVN_GET_IGNORES + "\"" + libDirName + "\"");
324
325 if (newIgnores.length > 0) {
326 Arrays.sort(newIgnores);
327 }
328 }
329 finally {
330 if (tempFile != null) {
331 FileUtil.delete(tempFile);
332 }
333 }
334 }
335
336 private void _addClasspathEntry(StringBundler sb, String jar)
337 throws Exception {
338
339 sb.append("\t<classpathentry kind=\"lib\" path=\"");
340 sb.append(jar);
341 sb.append("\" />\n");
342 }
343
344 private String[] _exec(String cmd) throws Exception {
345 Process process = Runtime.getRuntime().exec(cmd);
346
347 String[] stdout = _getExecOutput(process.getInputStream());
348 String[] stderr = _getExecOutput(process.getErrorStream());
349
350 if (stderr.length > 0) {
351 StringBundler sb = new StringBundler(stderr.length * 3 + 3);
352
353 sb.append("Received errors in executing '");
354 sb.append(cmd);
355 sb.append("'\n");
356
357 for (String err : stderr) {
358 sb.append("\t");
359 sb.append(err);
360 sb.append("\n");
361 }
362
363 throw new Exception(sb.toString());
364 }
365
366 return stdout;
367 }
368
369 private String[] _getExecOutput(InputStream is) throws IOException {
370 List<String> list = new ArrayList<String>();
371
372 UnsyncBufferedReader unsyncBufferedReader = null;
373
374 try {
375 unsyncBufferedReader = new UnsyncBufferedReader(
376 new InputStreamReader(is));
377
378 String line = unsyncBufferedReader.readLine();
379
380 while (line != null) {
381 line = line.trim();
382
383 if (Validator.isNotNull(line)) {
384 list.add(line);
385 }
386
387 line = unsyncBufferedReader.readLine();
388 }
389 }
390 finally {
391 if (unsyncBufferedReader != null) {
392 try {
393 unsyncBufferedReader.close();
394 }
395 catch (Exception e) {
396 }
397 }
398 }
399
400 return list.toArray(new String[] {});
401 }
402
403 private boolean _isSVNDir(File libDir) {
404 if (!libDir.exists()) {
405 return false;
406 }
407
408 try {
409 _exec(_SVN_INFO + "\"" + libDir + "\"");
410 }
411 catch (Exception e) {
412 return false;
413 }
414
415 return true;
416 }
417
418 private static final String _SVN_ADD = "svn add ";
419
420 private static final String _SVN_DEL_IGNORES = "svn propdel svn:ignore ";
421
422 private static final String _SVN_GET_IGNORES = "svn propget svn:ignore ";
423
424 private static final String _SVN_INFO = "svn info ";
425
426 private static final String _SVN_SET_IGNORES = "svn propset svn:ignore ";
427
428 private boolean _svn;
429
430 }