001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.tools.registration;
018:
019: import java.io.File;
020: import java.io.FileReader;
021:
022: import org.apache.commons.configuration.PropertiesConfiguration;
023: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
024: import org.apache.jetspeed.engine.JetspeedEngineConstants;
025: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
026: import org.apache.jetspeed.om.common.portlet.PortletApplication;
027: import org.apache.jetspeed.om.common.servlet.MutableWebApplication;
028: import org.apache.jetspeed.util.descriptor.ExtendedPortletMetadata;
029: import org.apache.jetspeed.util.descriptor.PortletApplicationDescriptor;
030: import org.apache.jetspeed.util.descriptor.WebApplicationDescriptor;
031: import org.springframework.context.ApplicationContext;
032: import org.springframework.context.support.ClassPathXmlApplicationContext;
033:
034: /**
035: * <p>
036: * OjbPortletRegistry
037: * </p>
038: * <p>
039: *
040: * </p>
041: *
042: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
043: * @version $Id: PersistenceBrokerPortletRegistry.java 225622 2005-07-27 20:39:14Z weaver $
044: *
045: */
046: public class RegistrationTool {
047: private PortletRegistry registry;
048:
049: public static void main(String args[]) {
050: String fileName = System.getProperty(
051: "org.apache.jetspeed.portletregistry.configuration",
052: "registration.properties");
053: PropertiesConfiguration configuration = new PropertiesConfiguration();
054: try {
055: File appRootDir = new File("./src/webapp");
056: System.setProperty(
057: JetspeedEngineConstants.APPLICATION_ROOT_KEY,
058: appRootDir.getAbsolutePath());
059: configuration.load(fileName);
060: String[] bootAssemblies = configuration
061: .getStringArray("boot.assemblies");
062: String[] assemblies = configuration
063: .getStringArray("assemblies");
064: ClassPathXmlApplicationContext ctx;
065:
066: if (bootAssemblies != null) {
067: ApplicationContext bootContext = new ClassPathXmlApplicationContext(
068: bootAssemblies, true);
069: ctx = new ClassPathXmlApplicationContext(assemblies,
070: true, bootContext);
071: } else {
072: ctx = new ClassPathXmlApplicationContext(assemblies,
073: true);
074: }
075:
076: boolean overwriteApps = configuration.getBoolean(
077: "overwrite.apps", true);
078: String registryBean = configuration.getString(
079: "registry.component", "");
080: String[] appNames = configuration.getStringArray("apps");
081: String[] appDescriptors = configuration
082: .getStringArray("descriptors");
083: String[] webappDescriptors = configuration
084: .getStringArray("webapp.descriptors");
085: String[] extendedDescriptors = configuration
086: .getStringArray("extended.descriptors");
087: PortletRegistry registry = (PortletRegistry) ctx
088: .getBean(registryBean);
089: RegistrationTool tool = new RegistrationTool(registry,
090: overwriteApps);
091:
092: for (int ix = 0; ix < appNames.length; ix++) {
093: if (overwriteApps) {
094: tool.unregister(appNames[ix]);
095: }
096: tool.register(appNames[ix], appDescriptors[ix],
097: webappDescriptors[ix], extendedDescriptors[ix]);
098: }
099: } catch (Exception e) {
100: System.err.println("Failed to import: " + e);
101: e.printStackTrace();
102: }
103:
104: }
105:
106: public RegistrationTool(PortletRegistry registry,
107: boolean overwriteApps) {
108: this .registry = registry;
109: }
110:
111: public void unregister(String appName) throws Exception {
112: if (registry.portletApplicationExists(appName)) {
113: PortletApplication app = registry
114: .getPortletApplication(appName);
115: if (app != null) {
116: registry.removeApplication(app);
117: }
118: }
119: }
120:
121: public void register(String appName, String appDescriptor,
122: String webappDescriptor, String extendedDescriptor)
123: throws Exception {
124: WebApplicationDescriptor wad = new WebApplicationDescriptor(
125: new FileReader(webappDescriptor), "/" + appName);
126: MutableWebApplication webapp = wad.createWebApplication();
127: PortletApplicationDescriptor pad = new PortletApplicationDescriptor(
128: new FileReader(appDescriptor), appName);
129: MutablePortletApplication app = pad.createPortletApplication();
130: app.setWebApplicationDefinition(webapp);
131: ExtendedPortletMetadata extMetaData = new ExtendedPortletMetadata(
132: new FileReader(extendedDescriptor), app);
133: extMetaData.load();
134: registry.registerPortletApplication(app);
135: }
136: }
|