View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.plugins;
20  
21  import java.util.Map;
22  import java.util.Set;
23  
24  public class Main
25  {
26      private static final String JETTY_HOME = "JETTY_HOME";
27  
28      private MavenService _mavenService = new HttpMavenService();
29      private PluginManager _pluginManager;
30      private String _jettyHome;
31      private String _installPlugin;
32      private boolean _listPlugins;
33      private String _repositoryUrl;
34      private String _groupId;
35      private String _version;
36      private boolean _searchRemoteRepository = true;
37      private boolean _searchLocalRepository = false;
38  
39      public static void main(String[] args)
40      {
41          Main main = new Main();
42          main.execute(args);
43      }
44  
45      private void execute(String[] args)
46      {
47          parseEnvironmentVariables();
48          parseCommandline(args);
49          configureMavenService();
50  
51          _pluginManager = new DefaultPluginManager(_mavenService, _jettyHome);
52  
53          if (_listPlugins)
54              listPlugins();
55          else if (_installPlugin != null)
56              installPlugin();
57      }
58  
59      private void configureMavenService()
60      {
61          if (_repositoryUrl != null)
62              _mavenService.setRepositoryUrl(_repositoryUrl);
63          if (_groupId != null)
64              _mavenService.setGroupId(_groupId);
65          if (_version != null)
66              _mavenService.setVersion(_version);
67  
68          _mavenService.setSearchLocalRepository(_searchLocalRepository);
69          _mavenService.setSearchRemoteRepository(_searchRemoteRepository);
70      }
71  
72      private void listPlugins()
73      {
74          Set<String> availablePlugins = _pluginManager.listAvailablePlugins();
75          for (String pluginName : availablePlugins)
76              System.out.println(pluginName);
77      }
78  
79      private void installPlugin()
80      {
81          _pluginManager.installPlugin(_installPlugin);
82          System.out.println("Successfully installed plugin: " + _installPlugin
83                  + " to " + _jettyHome);
84      }
85  
86      private void parseEnvironmentVariables()
87      {
88          Map<String, String> env = System.getenv();
89          if (env.containsKey(JETTY_HOME))
90              _jettyHome = env.get(JETTY_HOME);
91      }
92  
93      private void parseCommandline(String[] args)
94      {
95          int i = 0;
96          for (String arg : args)
97          {
98              i++;
99  
100             if (arg.startsWith("--jettyHome="))
101                 _jettyHome = arg.substring(12);
102             if (arg.startsWith("--repositoryUrl="))
103                 _repositoryUrl = arg.substring(16);
104             if (arg.startsWith("--groupId="))
105                 _groupId = arg.substring(10);
106             if (arg.startsWith("--version="))
107                 _version = arg.substring(10);
108             if (arg.startsWith("--useLocalRepo="))
109                 _searchLocalRepository = Boolean.valueOf(arg.substring(15));
110             if (arg.startsWith("--useRemoteRepo="))
111                 _searchRemoteRepository = Boolean.valueOf(arg.substring(15));
112             if (arg.startsWith("install"))
113                 _installPlugin = args[i];
114             if ("list".equals(arg))
115                 _listPlugins = true;
116         }
117 
118         if (_jettyHome == null && _installPlugin != null)
119             printUsage("No --jettyHome commandline option specified and no \"JETTY_HOME\" environment variable found!");
120         if (_installPlugin == null && !_listPlugins)
121             printUsage("Neither install <pluginname> nor list commandline option specified. Nothing to do for me!");
122         if (_installPlugin != null && _listPlugins)
123             printUsage("Please specify either install <pluginname> or list commandline options, " +
124                     "but not both at the same time!");
125     }
126 
127     private void printUsage(String errorMessage)
128     {
129         System.out.println(errorMessage);
130         System.exit(1);
131         //TODO: print usage
132     }
133 }