1
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
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
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
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 List<String> customJars = ListUtil.toList(
165 libDir.list(new GlobFilenameFilter("*.jar")));
166
167 Collections.sort(customJars);
168
169 for (String jar : dependencyJars) {
170 customJars.remove(jar);
171 }
172
173 sb = new StringBuilder();
174
175 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
176 sb.append("<classpath>\n");
177 sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
178 sb.append("kind=\"src\" path=\"docroot/WEB-INF/src\" />\n");
179 sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n");
180 sb.append("\t<classpathentry kind=\"con\" ");
181 sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n");
182
183 _addClasspathEntry(sb, "/portal/lib/development/mail.jar");
184 _addClasspathEntry(sb, "/portal/lib/development/servlet.jar");
185 _addClasspathEntry(sb, "/portal/lib/global/container.jar");
186 _addClasspathEntry(sb, "/portal/lib/global/portlet-container.jar");
187 _addClasspathEntry(sb, "/portal/lib/global/portlet.jar");
188
189 for (String jar : portalJars) {
190 _addClasspathEntry(sb, "/portal/lib/portal/" + jar);
191 }
192
193 _addClasspathEntry(sb, "/portal/portal-kernel/portal-kernel.jar");
194 _addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
195 _addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
196 _addClasspathEntry(sb, "/portal/util-java/util-java.jar");
197 _addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
198
199 for (String jar : customJars) {
200 _addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
201 }
202
203 sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
204 sb.append("</classpath>");
205
206 File classpathFile = new File(projectPath + "/.classpath");
207
208 System.out.println("Updating " + classpathFile);
209
210 _fileUtil.write(classpathFile, sb.toString());
211
212
214 if (_svn) {
215 try {
216 _exec(_SVN_INFO + projectFile);
217 }
218 catch (Exception e) {
219 _exec(_SVN_ADD + projectFile);
220 }
221
222 try {
223 _exec(_SVN_INFO + classpathFile);
224 }
225 catch (Exception e) {
226 _exec(_SVN_ADD + classpathFile);
227 }
228
229 _exec(_SVN_SET_IGNORES + "bin " + projectPath);
230 }
231 }
232
233 public void updateLibIgnores(File libDir, String[] jars) throws Exception {
234 if (!_isSVNDir(libDir)) {
235 return;
236 }
237
238 File tempFile = null;
239
240 try {
241 String libPath = libDir.getCanonicalPath();
242
243 String[] oldIgnores = _exec(_SVN_GET_IGNORES + libPath);
244
245 Arrays.sort(oldIgnores);
246
247 if (Arrays.equals(oldIgnores, jars)) {
248 return;
249 }
250
251 tempFile = File.createTempFile("svn-ignores-", null, null);
252
253 _exec(_SVN_DEL_IGNORES + libPath);
254
255 StringBuilder sb = new StringBuilder();
256
257 for (String jar : jars) {
258 sb.append(jar + "\n");
259 }
260
261 _fileUtil.write(tempFile, sb.toString());
262
263 _exec(
264 _SVN_SET_IGNORES + "-F \"" + tempFile.getCanonicalPath() +
265 "\" " + libPath);
266
267 String[] newIgnores = _exec(_SVN_GET_IGNORES + libPath);
268
269 if (newIgnores.length > 0) {
270 Arrays.sort(newIgnores);
271 }
272 }
273 finally {
274 if (tempFile != null) {
275 tempFile.delete();
276 }
277 }
278 }
279
280 private void _addClasspathEntry(StringBuilder sb, String jar)
281 throws Exception {
282
283 sb.append("\t<classpathentry kind=\"lib\" path=\"");
284 sb.append(jar);
285 sb.append("\" />\n");
286 }
287
288 private String[] _exec(String cmd) throws Exception {
289 Process process = Runtime.getRuntime().exec(cmd);
290
291 String[] stdout = _getExecOutput(process.getInputStream());
292 String[] stderr = _getExecOutput(process.getErrorStream());
293
294 if (stderr.length > 0) {
295 StringBuilder sb = new StringBuilder();
296
297 sb.append("Received errors in executing '" + cmd + "'\n");
298
299 for (String err : stderr) {
300 sb.append("\t" + err + "\n");
301 }
302
303 throw new Exception(sb.toString());
304 }
305
306 return stdout;
307 }
308
309 private String[] _getExecOutput(InputStream is) throws IOException {
310 List<String> list = new ArrayList<String>();
311
312 BufferedReader br = null;
313
314 try {
315 br = new BufferedReader(new InputStreamReader(is));
316
317 String line = br.readLine();
318
319 while (line != null) {
320 line = line.trim();
321
322 if (Validator.isNotNull(line)) {
323 list.add(line);
324 }
325
326 line = br.readLine();
327 }
328 }
329 finally {
330 if (br != null) {
331 try {
332 br.close();
333 }
334 catch (Exception e) {
335 }
336 }
337 }
338
339 return list.toArray(new String[] {});
340 }
341
342 private boolean _isSVNDir(File libDir) {
343 if (!libDir.exists()) {
344 return false;
345 }
346
347 try {
348 _exec(_SVN_INFO + libDir);
349 }
350 catch (Exception e) {
351 return false;
352 }
353
354 return true;
355 }
356
357 private static final String _SVN_ADD = "svn add ";
358
359 private static final String _SVN_DEL_IGNORES = "svn propdel svn:ignore ";
360
361 private static final String _SVN_GET_IGNORES = "svn propget svn:ignore ";
362
363 private static final String _SVN_INFO = "svn info ";
364
365 private static final String _SVN_SET_IGNORES = "svn propset svn:ignore ";
366
367 private static FileImpl _fileUtil = new FileImpl();
368
369 private boolean _svn;
370
371 }