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.pluto.util.assemble.io;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.util.Iterator;
023:
024: import org.apache.pluto.descriptors.common.InitParamDD;
025: import org.apache.pluto.descriptors.portlet.PortletAppDD;
026: import org.apache.pluto.descriptors.portlet.PortletDD;
027: import org.apache.pluto.descriptors.services.PortletAppDescriptorService;
028: import org.apache.pluto.descriptors.services.WebAppDescriptorService;
029: import org.apache.pluto.descriptors.services.castor.PortletAppDescriptorServiceImpl;
030: import org.apache.pluto.descriptors.services.castor.WebAppDescriptorServiceImpl;
031: import org.apache.pluto.descriptors.servlet.LoadOnStartupDD;
032: import org.apache.pluto.descriptors.servlet.ServletDD;
033: import org.apache.pluto.descriptors.servlet.ServletMappingDD;
034: import org.apache.pluto.descriptors.servlet.WebAppDD;
035: import org.apache.pluto.util.assemble.Assembler;
036:
037: /**
038: * Utility class responsible for accepting web.xml and portlet.xml as InputStreams, and assembling
039: * the web.xml to an OutputStream.
040: */
041: public class WebXmlStreamingAssembly {
042:
043: /**
044: * Assembles the web.xml represented by the <code>InputStream</code>.
045: *
046: * @param webXmlIn the unassembled web.xml file
047: * @param portletXmlIn the corresponding portlet.xml file
048: * @param assembledWebXmlOut the assembled web.xml file
049: * @param dispatchServletClass the dispatch servlet
050: * @throws IOException
051: */
052: public static void assembleStream(InputStream webXmlIn,
053: InputStream portletXmlIn, OutputStream assembledWebXmlOut,
054: String dispatchServletClass) throws IOException {
055: if (dispatchServletClass == null
056: || dispatchServletClass.length() == 0
057: || dispatchServletClass.trim().length() == 0) {
058: dispatchServletClass = Assembler.DISPATCH_SERVLET_CLASS;
059: }
060:
061: WebAppDescriptorService descriptorSvc = new WebAppDescriptorServiceImpl();
062: PortletAppDescriptorService portletAppDescriptorSvc = new PortletAppDescriptorServiceImpl();
063:
064: WebAppDD webAppDDIn = descriptorSvc.read(webXmlIn);
065: PortletAppDD portletAppDD = portletAppDescriptorSvc
066: .read(portletXmlIn);
067: portletXmlIn.close();
068:
069: for (Iterator it = portletAppDD.getPortlets().iterator(); it
070: .hasNext();) {
071:
072: // Read portlet definition.
073: PortletDD portlet = (PortletDD) it.next();
074: String name = portlet.getPortletName();
075:
076: ServletDD servlet = new ServletDD();
077: servlet.setServletName(name);
078:
079: servlet.setServletClass(dispatchServletClass);
080:
081: InitParamDD initParam = new InitParamDD();
082: initParam.setParamName("portlet-name");
083: initParam.setParamValue(name);
084: servlet.getInitParams().add(initParam);
085:
086: LoadOnStartupDD onStartup = new LoadOnStartupDD();
087: onStartup.setPriority(1);
088: servlet.setLoadOnStartup(onStartup);
089:
090: ServletMappingDD servletMapping = new ServletMappingDD();
091: servletMapping.setServletName(name);
092: servletMapping.setUrlPattern("/PlutoInvoker/" + name);
093:
094: webAppDDIn.getServlets().add(servlet);
095: webAppDDIn.getServletMappings().add(servletMapping);
096:
097: }
098:
099: descriptorSvc.write(webAppDDIn, assembledWebXmlOut);
100:
101: }
102: }
|