001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.ListUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.UniqueList;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.FileImpl;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.io.File;
027    import java.io.FileInputStream;
028    import java.io.FilenameFilter;
029    
030    import java.util.ArrayList;
031    import java.util.Arrays;
032    import java.util.Collections;
033    import java.util.HashMap;
034    import java.util.List;
035    import java.util.Map;
036    import java.util.Properties;
037    import java.util.Set;
038    import java.util.TreeSet;
039    
040    import org.apache.oro.io.GlobFilenameFilter;
041    import org.apache.tools.ant.DirectoryScanner;
042    
043    /**
044     * @author Alexander Chow
045     * @author Brian Wing Shun Chan
046     */
047    public class PluginsEnvironmentBuilder {
048    
049            public static void main(String[] args) throws Exception {
050                    try {
051                            File dir = new File(System.getProperty("plugins.env.dir"));
052    
053                            new PluginsEnvironmentBuilder(dir);
054                    }
055                    catch (Exception e) {
056                            e.printStackTrace();
057                    }
058            }
059    
060            public PluginsEnvironmentBuilder(File dir) throws Exception {
061                    DirectoryScanner directoryScanner = new DirectoryScanner();
062    
063                    directoryScanner.setBasedir(dir);
064                    directoryScanner.setIncludes(
065                            new String[] {"**\\liferay-plugin-package.properties"});
066    
067                    directoryScanner.scan();
068    
069                    String dirName = dir.getCanonicalPath();
070    
071                    for (String fileName : directoryScanner.getIncludedFiles()) {
072                            setupWarProject(dirName, fileName);
073                    }
074    
075                    directoryScanner = new DirectoryScanner();
076    
077                    directoryScanner.setBasedir(dir);
078                    directoryScanner.setIncludes(new String[] {"**\\build.xml"});
079    
080                    directoryScanner.scan();
081    
082                    for (String fileName : directoryScanner.getIncludedFiles()) {
083                            String content = _fileUtil.read(dirName + "/" + fileName);
084    
085                            boolean osgiProject = content.contains(
086                                    "<import file=\"../../build-common-osgi-plugin.xml\" />");
087                            boolean sharedProject = content.contains(
088                                    "<import file=\"../build-common-shared.xml\" />");
089    
090                            List<String> dependencyJars = Collections.emptyList();
091    
092                            if (osgiProject) {
093                                    int x = content.indexOf("osgi.plugin.portal.lib.jars");
094    
095                                    if (x != -1) {
096                                            x = content.indexOf("value=\"", x);
097                                            x = content.indexOf("\"", x);
098    
099                                            int y = content.indexOf("\"", x + 1);
100    
101                                            dependencyJars = Arrays.asList(
102                                                    StringUtil.split(content.substring(x + 1, y)));
103                                    }
104                            }
105    
106                            if (osgiProject || sharedProject) {
107                                    setupJarProject(
108                                            dirName, fileName, dependencyJars, sharedProject);
109                            }
110                    }
111            }
112    
113            protected void addClasspathEntry(StringBundler sb, String jar) {
114                    addClasspathEntry(sb, jar, null);
115            }
116    
117            protected void addClasspathEntry(
118                    StringBundler sb, String jar, Map<String, String> attributes) {
119    
120                    sb.append("\t<classpathentry kind=\"lib\" path=\"");
121                    sb.append(jar);
122    
123                    if ((attributes == null) || attributes.isEmpty()) {
124                            sb.append("\" />\n");
125    
126                            return;
127                    }
128    
129                    sb.append("\">\n\t\t<attributes>\n");
130    
131                    for (Map.Entry<String, String> entry : attributes.entrySet()) {
132                            sb.append("\t\t\t<attribute name=\"");
133                            sb.append(entry.getKey());
134                            sb.append("\" value=\"");
135                            sb.append(entry.getValue());
136                            sb.append("\" />\n");
137                    }
138    
139                    sb.append("\t\t</attributes>\n\t</classpathentry>\n");
140            }
141    
142            protected List<String> getCommonJars() {
143                    List<String> jars = new ArrayList<String>();
144    
145                    jars.add("commons-logging.jar");
146                    jars.add("log4j.jar");
147                    jars.add("util-bridges.jar");
148                    jars.add("util-java.jar");
149                    jars.add("util-taglib.jar");
150    
151                    return jars;
152            }
153    
154            protected List<String> getImportSharedJars(File projectDir)
155                    throws Exception {
156    
157                    File buildXmlFile = new File(projectDir, "build.xml");
158    
159                    String content = _fileUtil.read(buildXmlFile);
160    
161                    int x = content.indexOf("import.shared");
162    
163                    if (x == -1) {
164                            return new ArrayList<String>();
165                    }
166    
167                    x = content.indexOf("value=\"", x);
168                    x = content.indexOf("\"", x);
169    
170                    int y = content.indexOf("\" />", x);
171    
172                    if ((x == -1) || (y == -1)) {
173                            return new ArrayList<String>();
174                    }
175    
176                    String[] importShared = StringUtil.split(content.substring(x + 1, y));
177    
178                    if (importShared.length == 0) {
179                            return new ArrayList<String>();
180                    }
181    
182                    List<String> jars = new ArrayList<String>();
183    
184                    for (String currentImportShared : importShared) {
185                            jars.add(currentImportShared + ".jar");
186    
187                            File currentImportSharedLibDir = new File(
188                                    projectDir, "/../../shared/" + currentImportShared + "/lib");
189    
190                            if (!currentImportSharedLibDir.exists()) {
191                                    continue;
192                            }
193    
194                            for (File file : currentImportSharedLibDir.listFiles()) {
195                                    jars.add(file.getName());
196                            }
197                    }
198    
199                    return jars;
200            }
201    
202            protected List<String> getPortalDependencyJars(Properties properties) {
203                    String[] dependencyJars = StringUtil.split(
204                            properties.getProperty(
205                                    "portal-dependency-jars",
206                                    properties.getProperty("portal.dependency.jars")));
207    
208                    return ListUtil.toList(dependencyJars);
209            }
210    
211            protected List<String> getRequiredDeploymentContextsJars(
212                            File libDir, Properties properties)
213                    throws Exception {
214    
215                    List<String> jars = new ArrayList<String>();
216    
217                    String[] requiredDeploymentContexts = StringUtil.split(
218                            properties.getProperty("required-deployment-contexts"));
219    
220                    for (String requiredDeploymentContext : requiredDeploymentContexts) {
221                            if (_fileUtil.exists(
222                                            libDir.getCanonicalPath() + "/" +
223                                                    requiredDeploymentContext + "-service.jar")) {
224    
225                                    jars.add(requiredDeploymentContext + "-service.jar");
226                            }
227                    }
228    
229                    return jars;
230            }
231    
232            protected void setupJarProject(
233                            String dirName, String fileName, List<String> dependencyJars,
234                            boolean sharedProject)
235                    throws Exception {
236    
237                    File buildFile = new File(dirName + "/" + fileName);
238    
239                    File projectDir = new File(buildFile.getParent());
240    
241                    File libDir = new File(projectDir, "lib");
242    
243                    writeEclipseFiles(libDir, projectDir, dependencyJars);
244    
245                    List<String> importSharedJars = getImportSharedJars(projectDir);
246    
247                    if (sharedProject) {
248                            if (!importSharedJars.contains("portal-compat-shared.jar")) {
249                                    importSharedJars.add("portal-compat-shared.jar");
250                            }
251                    }
252    
253                    File gitignoreFile = new File(
254                            projectDir.getCanonicalPath() + "/.gitignore");
255    
256                    String[] gitIgnores = importSharedJars.toArray(
257                            new String[importSharedJars.size()]);
258    
259                    for (int i = 0; i < gitIgnores.length; i++) {
260                            String gitIgnore = gitIgnores[i];
261    
262                            gitIgnore = "/lib/" + gitIgnore;
263    
264                            gitIgnores[i] = gitIgnore;
265                    }
266    
267                    if (gitIgnores.length > 0) {
268                            System.out.println("Updating " + gitignoreFile);
269    
270                            _fileUtil.write(gitignoreFile, StringUtil.merge(gitIgnores, "\n"));
271                    }
272            }
273    
274            protected void setupWarProject(String dirName, String fileName)
275                    throws Exception {
276    
277                    File propertiesFile = new File(dirName + "/" + fileName);
278    
279                    Properties properties = new Properties();
280    
281                    properties.load(new FileInputStream(propertiesFile));
282    
283                    Set<String> jars = new TreeSet<String>();
284    
285                    jars.addAll(getCommonJars());
286    
287                    List<String> dependencyJars = getPortalDependencyJars(properties);
288    
289                    jars.addAll(dependencyJars);
290    
291                    File projectDir = new File(propertiesFile.getParent() + "/../..");
292    
293                    jars.addAll(getImportSharedJars(projectDir));
294    
295                    File libDir = new File(propertiesFile.getParent() + "/lib");
296    
297                    jars.addAll(getRequiredDeploymentContextsJars(libDir, properties));
298    
299                    writeEclipseFiles(libDir, projectDir, dependencyJars);
300    
301                    String libDirPath = StringUtil.replace(
302                            libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH);
303    
304                    List<String> ignores = ListUtil.fromFile(
305                            libDir.getCanonicalPath() + "/../.gitignore");
306    
307                    if (libDirPath.contains("/ext/") || ignores.contains("/lib")) {
308                            return;
309                    }
310    
311                    File gitignoreFile = new File(
312                            libDir.getCanonicalPath() + "/.gitignore");
313    
314                    System.out.println("Updating " + gitignoreFile);
315    
316                    String[] gitIgnores = jars.toArray(new String[jars.size()]);
317    
318                    for (int i = 0; i < gitIgnores.length; i++) {
319                            String gitIgnore = gitIgnores[i];
320    
321                            if (Validator.isNotNull(gitIgnore) && !gitIgnore.startsWith("/")) {
322                                    gitIgnores[i] = "/" + gitIgnore;
323                            }
324                    }
325    
326                    _fileUtil.write(gitignoreFile, StringUtil.merge(gitIgnores, "\n"));
327            }
328    
329            protected void writeClasspathFile(
330                            File libDir, List<String> dependencyJars, String projectDirName,
331                            String projectName, boolean javaProject)
332                    throws Exception {
333    
334                    File classpathFile = new File(projectDirName + "/.classpath");
335    
336                    if (!javaProject) {
337                            classpathFile.delete();
338    
339                            return;
340                    }
341    
342                    List<String> globalJars = new UniqueList<String>();
343                    List<String> portalJars = new UniqueList<String>();
344    
345                    List<String> extGlobalJars = new UniqueList<String>();
346                    List<String> extPortalJars = new UniqueList<String>();
347    
348                    String libDirPath = StringUtil.replace(
349                            libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH);
350    
351                    if (libDirPath.contains("/ext/")) {
352                            FilenameFilter filenameFilter = new GlobFilenameFilter("*.jar");
353    
354                            for (String dirName : new String[] {"global", "portal"}) {
355                                    File file = new File(libDirPath + "/../ext-lib/" + dirName);
356    
357                                    List<String> jars = ListUtil.toList(file.list(filenameFilter));
358    
359                                    if (dirName.equals("global")) {
360                                            extGlobalJars.addAll(ListUtil.sort(jars));
361    
362                                            File dir = new File(PropsValues.LIFERAY_LIB_GLOBAL_DIR);
363    
364                                            String[] fileNames = dir.list(filenameFilter);
365    
366                                            globalJars.addAll(
367                                                    ListUtil.sort(ListUtil.toList(fileNames)));
368                                            globalJars.removeAll(extGlobalJars);
369                                    }
370                                    else if (dirName.equals("portal")) {
371                                            extPortalJars.addAll(ListUtil.sort(jars));
372    
373                                            File dir = new File(PropsValues.LIFERAY_LIB_PORTAL_DIR);
374    
375                                            String[] fileNames = dir.list(filenameFilter);
376    
377                                            portalJars.addAll(
378                                                    ListUtil.sort(ListUtil.toList(fileNames)));
379                                            portalJars.removeAll(extPortalJars);
380                                    }
381                            }
382                    }
383                    else {
384                            globalJars.add("portlet.jar");
385    
386                            portalJars.addAll(dependencyJars);
387                            portalJars.add("commons-logging.jar");
388                            portalJars.add("log4j.jar");
389    
390                            Collections.sort(portalJars);
391                    }
392    
393                    String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar"));
394    
395                    List<String> customJars = null;
396    
397                    if (customJarsArray != null) {
398                            customJars = ListUtil.toList(customJarsArray);
399    
400                            for (String jar : portalJars) {
401                                    customJars.remove(jar);
402                            }
403    
404                            customJars.remove(projectName + "-service.jar");
405                            customJars.remove("util-bridges.jar");
406                            customJars.remove("util-java.jar");
407                            customJars.remove("util-taglib.jar");
408    
409                            Collections.sort(customJars);
410                    }
411                    else {
412                            customJars = new ArrayList<String>();
413                    }
414    
415                    StringBundler sb = new StringBundler();
416    
417                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
418                    sb.append("<classpath>\n");
419    
420                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
421                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
422                                    sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
423                                    sb.append("kind=\"src\" path=\"");
424                                    sb.append(sourceDirName);
425                                    sb.append("\" />\n");
426                            }
427                    }
428    
429                    sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n");
430                    sb.append("\t<classpathentry kind=\"con\" ");
431                    sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n");
432    
433                    boolean addJunitJars = false;
434    
435                    for (String testType : _TEST_TYPES) {
436                            String testFolder = "test/" + testType;
437    
438                            if (_fileUtil.exists(projectDirName + "/" + testFolder)) {
439                                    addJunitJars = true;
440    
441                                    sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
442                                    sb.append("kind=\"src\" path=\"");
443                                    sb.append(testFolder);
444                                    sb.append("\" />\n");
445                            }
446                    }
447    
448                    if (addJunitJars) {
449                            addClasspathEntry(sb, "/portal/lib/development/junit.jar");
450                            addClasspathEntry(sb, "/portal/lib/development/mockito.jar");
451                            addClasspathEntry(
452                                    sb, "/portal/lib/development/powermock-mockito.jar");
453                            addClasspathEntry(sb, "/portal/lib/development/spring-test.jar");
454    
455                            portalJars.add("commons-io.jar");
456                            portalJars.add("commons-lang.jar");
457                    }
458    
459                    addClasspathEntry(sb, "/portal/lib/development/activation.jar");
460                    addClasspathEntry(sb, "/portal/lib/development/annotations.jar");
461                    addClasspathEntry(sb, "/portal/lib/development/jsp-api.jar");
462                    addClasspathEntry(sb, "/portal/lib/development/mail.jar");
463                    addClasspathEntry(sb, "/portal/lib/development/servlet-api.jar");
464    
465                    Map<String, String> attributes = new HashMap<String, String>();
466    
467                    if (libDirPath.contains("/ext/")) {
468                            attributes.put("optional", "true");
469                    }
470    
471                    for (String jar : globalJars) {
472                            addClasspathEntry(sb, "/portal/lib/global/" + jar, attributes);
473                    }
474    
475                    for (String jar : portalJars) {
476                            if (!jar.equals("util-slf4j.jar")) {
477                                    addClasspathEntry(sb, "/portal/lib/portal/" + jar, attributes);
478                            }
479                    }
480    
481                    addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
482                    addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
483                    addClasspathEntry(sb, "/portal/util-java/util-java.jar");
484    
485                    if (portalJars.contains("util-slf4j.jar")) {
486                            addClasspathEntry(sb, "/portal/util-slf4j/util-slf4j.jar");
487                    }
488    
489                    addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
490    
491                    for (String jar : extGlobalJars) {
492                            addClasspathEntry(sb, "docroot/WEB-INF/ext-lib/global/" + jar);
493                    }
494    
495                    for (String jar : extPortalJars) {
496                            addClasspathEntry(sb, "docroot/WEB-INF/ext-lib/portal/" + jar);
497                    }
498    
499                    for (String jar : customJars) {
500                            if (libDirPath.contains("/tmp/WEB-INF/lib")) {
501                                    addClasspathEntry(sb, "tmp/WEB-INF/lib/" + jar);
502                            }
503                            else if (libDirPath.contains("/docroot/WEB-INF/lib")) {
504                                    addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
505                            }
506                            else {
507                                    addClasspathEntry(sb, "lib/" + jar);
508                            }
509                    }
510    
511                    sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
512                    sb.append("</classpath>");
513    
514                    System.out.println("Updating " + classpathFile);
515    
516                    String content = StringUtil.replace(
517                            sb.toString(), "\"/portal", "\"/portal-" + _BRANCH);
518    
519                    _fileUtil.write(classpathFile, content);
520            }
521    
522            protected void writeEclipseFiles(
523                            File libDir, File projectDir, List<String> dependencyJars)
524                    throws Exception {
525    
526                    String projectDirName = projectDir.getCanonicalPath();
527    
528                    String projectName = StringUtil.extractLast(
529                            projectDirName, File.separatorChar);
530    
531                    boolean javaProject = false;
532    
533                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
534                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
535                                    javaProject = true;
536    
537                                    break;
538                            }
539                    }
540    
541                    if (!javaProject) {
542                            System.out.println(
543                                    "Eclipse Java project will not be used because a source " +
544                                            "folder does not exist");
545                    }
546    
547                    writeProjectFile(projectDirName, projectName, javaProject);
548    
549                    writeClasspathFile(
550                            libDir, dependencyJars, projectDirName, projectName, javaProject);
551    
552                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
553                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
554                                    List<String> gitIgnores = new ArrayList<String>();
555    
556                                    if (sourceDirName.endsWith("ext-impl/src")) {
557                                            gitIgnores.add("/classes");
558                                            gitIgnores.add("/ext-impl.jar");
559                                    }
560                                    else if (sourceDirName.endsWith("ext-service/src")) {
561                                            gitIgnores.add("/classes");
562                                            gitIgnores.add("/ext-service.jar");
563                                    }
564                                    else if (sourceDirName.endsWith("ext-util-bridges/src")) {
565                                            gitIgnores.add("/classes");
566                                            gitIgnores.add("/ext-util-bridges.jar");
567                                    }
568                                    else if (sourceDirName.endsWith("ext-util-java/src")) {
569                                            gitIgnores.add("/classes");
570                                            gitIgnores.add("/ext-util-java.jar");
571                                    }
572                                    else if (sourceDirName.endsWith("ext-util-taglib/src")) {
573                                            gitIgnores.add("/classes");
574                                            gitIgnores.add("/ext-util-taglib.jar");
575                                    }
576                                    else {
577                                            continue;
578                                    }
579    
580                                    String dirName = projectDirName + "/" + sourceDirName + "/../";
581    
582                                    if (gitIgnores.isEmpty()) {
583                                            _fileUtil.delete(dirName + ".gitignore");
584                                    }
585                                    else {
586                                            String gitIgnoresString = StringUtil.merge(
587                                                    gitIgnores, "\n");
588    
589                                            _fileUtil.write(dirName + ".gitignore", gitIgnoresString);
590                                    }
591                            }
592                    }
593    
594                    if (_fileUtil.exists(projectDirName + "/test")) {
595                            _fileUtil.write(
596                                    projectDirName + "/.gitignore", "/test-classes\n/test-results");
597                    }
598                    else {
599                            _fileUtil.delete(projectDirName + "/.gitignore");
600                    }
601            }
602    
603            protected void writeProjectFile(
604                            String projectDirName, String projectName, boolean javaProject)
605                    throws Exception {
606    
607                    StringBundler sb = new StringBundler(17);
608    
609                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
610                    sb.append("<projectDescription>\n");
611                    sb.append("\t<name>");
612                    sb.append(projectName);
613                    sb.append("-");
614                    sb.append(_BRANCH);
615                    sb.append("</name>\n");
616                    sb.append("\t<comment></comment>\n");
617                    sb.append("\t<projects></projects>\n");
618                    sb.append("\t<buildSpec>\n");
619    
620                    if (javaProject) {
621                            sb.append("\t\t<buildCommand>\n");
622                            sb.append("\t\t\t<name>org.eclipse.jdt.core.javabuilder</name>\n");
623                            sb.append("\t\t\t<arguments></arguments>\n");
624                            sb.append("\t\t</buildCommand>\n");
625                    }
626    
627                    sb.append("\t</buildSpec>\n");
628                    sb.append("\t<natures>\n");
629    
630                    if (javaProject) {
631                            sb.append("\t\t<nature>org.eclipse.jdt.core.javanature</nature>\n");
632                    }
633    
634                    sb.append("\t</natures>\n");
635                    sb.append("</projectDescription>");
636    
637                    File projectFile = new File(projectDirName + "/.project");
638    
639                    System.out.println("Updating " + projectFile);
640    
641                    _fileUtil.write(projectFile, sb.toString());
642            }
643    
644            private static final String _BRANCH = "master";
645    
646            private static final String[] _SOURCE_DIR_NAMES = new String[] {
647                    "docroot/WEB-INF/ext-impl/src", "docroot/WEB-INF/ext-service/src",
648                    "docroot/WEB-INF/ext-util-bridges/src",
649                    "docroot/WEB-INF/ext-util-java/src",
650                    "docroot/WEB-INF/ext-util-taglib/src", "docroot/WEB-INF/service",
651                    "docroot/WEB-INF/src", "src"
652            };
653    
654            private static final String[] _TEST_TYPES = {"integration", "unit"};
655    
656            private static FileImpl _fileUtil = FileImpl.getInstance();
657    
658    }