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