View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.tools.registration;
18  
19  import java.io.File;
20  import java.io.FileReader;
21  
22  import org.apache.commons.configuration.PropertiesConfiguration;
23  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
24  import org.apache.jetspeed.engine.JetspeedEngineConstants;
25  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
26  import org.apache.jetspeed.om.common.portlet.PortletApplication;
27  import org.apache.jetspeed.om.common.servlet.MutableWebApplication;
28  import org.apache.jetspeed.util.descriptor.ExtendedPortletMetadata;
29  import org.apache.jetspeed.util.descriptor.PortletApplicationDescriptor;
30  import org.apache.jetspeed.util.descriptor.WebApplicationDescriptor;
31  import org.springframework.context.ApplicationContext;
32  import org.springframework.context.support.ClassPathXmlApplicationContext;
33  
34  /***
35   * <p>
36   * OjbPortletRegistry
37   * </p>
38   * <p>
39   * 
40   * </p>
41   * 
42   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
43   * @version $Id: PersistenceBrokerPortletRegistry.java 225622 2005-07-27 20:39:14Z weaver $
44   *  
45   */
46  public class RegistrationTool 
47  {
48      private PortletRegistry registry;
49      
50      public static void main(String args[])
51      {
52          String fileName = System.getProperty("org.apache.jetspeed.portletregistry.configuration", "registration.properties");
53          PropertiesConfiguration configuration = new PropertiesConfiguration();
54          try
55          {
56              File appRootDir = new File("./src/webapp");            
57              System.setProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, appRootDir.getAbsolutePath());            
58              configuration.load(fileName);        
59              String [] bootAssemblies = configuration.getStringArray("boot.assemblies");
60              String [] assemblies = configuration.getStringArray("assemblies");            
61              ClassPathXmlApplicationContext ctx;            
62              
63              if (bootAssemblies != null)
64              {
65                  ApplicationContext bootContext = new ClassPathXmlApplicationContext(bootAssemblies, true);
66                  ctx = new ClassPathXmlApplicationContext(assemblies, true, bootContext);
67              }
68              else
69              {
70                  ctx = new ClassPathXmlApplicationContext(assemblies, true);
71              }
72              
73              boolean overwriteApps = configuration.getBoolean("overwrite.apps", true);
74              String registryBean = configuration.getString("registry.component", "");
75              String[] appNames = configuration.getStringArray("apps");
76              String[] appDescriptors = configuration.getStringArray("descriptors");
77              String[] webappDescriptors = configuration.getStringArray("webapp.descriptors");
78              String[] extendedDescriptors = configuration.getStringArray("extended.descriptors");
79              PortletRegistry registry = (PortletRegistry)ctx.getBean(registryBean);
80              RegistrationTool tool = new RegistrationTool(registry, overwriteApps);
81              
82              for (int ix=0; ix < appNames.length; ix++)
83              {
84                  if (overwriteApps)
85                  {
86                      tool.unregister(appNames[ix]);
87                  }
88                  tool.register(appNames[ix], appDescriptors[ix], webappDescriptors[ix], extendedDescriptors[ix]);
89              }
90          }
91          catch (Exception e)
92          {
93              System.err.println("Failed to import: " + e);
94              e.printStackTrace();
95          }
96          
97      }
98      
99      public RegistrationTool(PortletRegistry registry, boolean overwriteApps)
100     {
101         this.registry = registry;
102     }
103     
104     public void unregister(String appName)
105     throws Exception
106     {
107         if (registry.portletApplicationExists(appName))
108         {
109             PortletApplication app = registry.getPortletApplication(appName);
110             if (app != null)
111             {
112                 registry.removeApplication(app);
113             }
114         }
115     }
116     
117     public void register(String appName, String appDescriptor, String webappDescriptor, String extendedDescriptor)
118     throws Exception
119     {
120         WebApplicationDescriptor wad = new WebApplicationDescriptor(new FileReader(webappDescriptor), "/" + appName);
121         MutableWebApplication webapp = wad.createWebApplication();
122         PortletApplicationDescriptor pad = new PortletApplicationDescriptor(new FileReader(appDescriptor), appName);        
123         MutablePortletApplication app = pad.createPortletApplication();                
124         app.setWebApplicationDefinition(webapp);
125         ExtendedPortletMetadata extMetaData = new ExtendedPortletMetadata(new FileReader(extendedDescriptor), app);
126         extMetaData.load();        
127         registry.registerPortletApplication(app);
128     }
129 }