View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.pluto.driver.deploy;
18  
19  import java.io.File;
20  import java.util.ArrayList;
21  
22  import org.apache.pluto.descriptors.services.PortletAppDescriptorService;
23  import org.apache.pluto.descriptors.services.WebAppDescriptorService;
24  import org.apache.pluto.descriptors.services.impl.FilePortletAppDescriptorServiceImpl;
25  import org.apache.pluto.descriptors.services.impl.FileWebAppDescriptorServiceImpl;
26  import org.apache.pluto.driver.deploy.impl.PortletEntityRegistryRegistrarService;
27  import org.apache.pluto.driver.deploy.impl.ContextRegistryRegistrarService;
28  
29  public class CLI {
30  
31      /***
32       * Private Constructor used to prevent instantiation.
33       */
34      private CLI() {
35  
36      }
37  
38      /***
39       * Command Line Interface for the Pluto Deploy tool.
40       * <pre>
41    Usage: deploy [options] [<portlet-webapp-dir> | <portlet-war-file>]"
42    Options:
43      -destination, -d       destination to which the app should be deployed
44      -portal, -p            specify the portal location
45      -version,              print versioning information
46      -verbose, -v           be extra verbose
47      -help, -h              print this messagae</pre>
48    Possible Future Options
49      -regsitrar, -r         class name of the registrar which should be used
50       * @param args
51       * @throws Exception
52       */
53      public static void main(String args[]) throws Exception {
54          CLIArgs cli = parseArgs(args);
55          // cli will be null if the -version or -help flags
56          // have been turned on.
57          if(cli!=null) {
58              Deploy deploy = createDeployer(cli);
59              deploy.deploy(cli.portletApplication);
60          }
61      }
62  
63      /***
64       * Create a deployer from the parsed arguments.
65       * @param args CLIArgs parsed from the command line.
66       * @return an instance of the Deployer.
67       */
68      private static Deploy createDeployer(CLIArgs args) {
69          ArrayList registrars = new ArrayList();
70          PortletApplicationExploder exploder = null;
71          WebAppDescriptorService webAppDescriptorService = null;
72          PortletAppDescriptorService portletAppDescriptorService = null;
73  
74          args.destinationDirectory.mkdirs();
75          if(!args.portletApplication.isDirectory()) {
76              exploder = new PortletApplicationExploder(args.destinationDirectory);
77          }
78  
79          if(args.portalApplication!=null) {
80              registrars.add(new PortletEntityRegistryRegistrarService(args.portalApplication));
81              registrars.add(new ContextRegistryRegistrarService(args.portalApplication));
82          }
83          if(args.debug) {
84              System.out.println("<VERBOSE> Portal WebApp: "+ args.portalApplication.getAbsolutePath());
85          }
86  
87  
88          int extLocation = args.portletApplication.getName().indexOf(".");
89          if(extLocation > 0) {
90              args.destinationDirectory = new File(args.destinationDirectory, args.portletApplication.getName().substring(0, extLocation));
91          }
92          else {
93              args.destinationDirectory = new File(args.destinationDirectory, args.portletApplication.getName());
94          }
95  
96          if(args.debug) {
97              System.out.println("<VERBOSE> Portlet Context: "+ args.destinationDirectory.getAbsolutePath());
98          }
99  
100         webAppDescriptorService = new FileWebAppDescriptorServiceImpl(args.destinationDirectory);
101         portletAppDescriptorService = new FilePortletAppDescriptorServiceImpl(args.destinationDirectory);
102 
103         Deploy deploy = new Deploy(webAppDescriptorService, portletAppDescriptorService);
104         deploy.setDebug(args.debug);
105         deploy.setExploder(exploder);
106         deploy.setRegistrars(registrars);
107         return deploy;
108     }
109 
110     /***
111      * Parse the command line arguments into the appropriate
112      * File objects.
113      * @param args
114      * @return
115      */
116     private static CLIArgs parseArgs(String[] args) {
117         CLIArgs result = new CLIArgs();
118 
119         for (int i=0;i<args.length;i++) {
120             String arg = args[i];
121 
122             if("-help".equals(arg) || "-h".equals(arg)) {
123                 printUsage();
124                 return null;
125             }
126             else if("-version".equals(arg)) {
127                 printVersion();
128                 return null;
129             }
130             else if("-verbose".equals(arg) || "-v".equals(arg)) {
131                 result.debug = true;
132             }
133             else if("-portal".equals(arg) || "-p".equals(arg)) {
134                 try {
135                     result.portalApplication = new File(args[++i]);
136                     if(!result.portalApplication.exists()) {
137                         throw new IllegalArgumentException(
138                             "'"+result.portalApplication.getAbsolutePath()+
139                             "' is not a valid portal path"
140                         );
141                     }
142                 }
143                 catch(ArrayIndexOutOfBoundsException ai) {
144                     throw new IllegalArgumentException(
145                         "-portal must be followed by the location of the portal"
146                     );
147                 }
148             }
149             else if("-destination".equals(arg) || "-d".equals(arg)) {
150                 result.destinationDirectory = new File(args[++i]);
151             }
152             else if (arg.startsWith("-")) {
153                 throw new IllegalArgumentException("Illegal Option: "+arg);
154             }
155             else {
156                 result.portletApplication = new File(args[i]);
157             }
158         }
159 
160         if(result.portletApplication == null) {
161             throw new IllegalArgumentException(
162                 "Portlet Application must be specified"
163             );
164         }
165 
166         if(result.debug) {
167             System.out.println("<VERBOSE> Source WebApp: "+ result.portletApplication.getAbsolutePath());
168         }
169 
170         if(result.destinationDirectory == null) {
171             result.destinationDirectory = result.portletApplication.getParentFile();
172         }
173 
174         if(result.debug) {
175             System.out.println("<VERBOSE> Destination:   "+ result.destinationDirectory.getAbsolutePath());
176         }
177         return result;
178     }
179 
180     /***
181      * Print command usage information.
182      */
183     private static void printUsage() {
184         String sep = System.getProperty("line.separator");
185         StringBuffer sb = new StringBuffer(sep);
186         sb.append("deploy [options] [<portlet-webapp-dir> | <portlet-war-file>]"+sep);
187         sb.append("Options: "+sep);
188         sb.append("  -destination, -d       destination to which the app should be deployed"+sep);
189         sb.append("  -portal, -p            specify the portal location"+sep);
190         sb.append("  -version,              print versioning information"+sep);
191         sb.append("  -verbose, -v           be extra verbose"+sep);
192         sb.append("  -help, -h              print this messagae"+sep);
193         sb.append("Future Options: "+sep);
194         sb.append("  -registrar, -r         the class name of the registrar used to register the portlets with the portal"+sep);
195         System.out.println(sb);
196     }
197 
198     /***
199      * Print version informaiton.
200      */
201     private static void printVersion() {
202         System.out.println("Apache Pluto Deploy / 1.0");
203     }
204 
205     /***
206      * Parged and converted Command Line arguments.
207      */
208     static class CLIArgs {
209         private File portletApplication;
210         private File portalApplication;
211         private File destinationDirectory;
212         private boolean debug;
213     }
214 
215 }