Home » geronimo-2.2-source-release » org.apache.geronimo.web.deployment » [javadoc | source]

    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.geronimo.web.deployment;
   18   
   19   import java.util.ArrayList;
   20   import java.util.HashMap;
   21   import java.util.List;
   22   import java.util.Map;
   23   import java.util.Set;
   24   import java.util.HashSet;
   25   
   26   import javax.xml.namespace.QName;
   27   
   28   import org.apache.geronimo.common.DeploymentException;
   29   import org.apache.geronimo.deployment.xbeans.ModuleDocument;
   30   import org.apache.geronimo.schema.SchemaConversionUtils;
   31   import org.apache.geronimo.xbeans.geronimo.security.GerSecurityDocument;
   32   import org.apache.geronimo.xbeans.geronimo.web.GerWebAppDocument;
   33   import org.apache.xmlbeans.XmlCursor;
   34   import org.apache.xmlbeans.XmlObject;
   35   
   36   /**
   37    * @version $Rev: 685070 $ $Date: 2008-08-12 00:27:25 -0700 (Tue, 12 Aug 2008) $
   38    */
   39   public class GenericToSpecificPlanConverter {
   40   
   41       private static final QName GENERIC_QNAME = GerWebAppDocument.type.getDocumentElementName();
   42       private static final String GENERIC_NAMESPACE = GENERIC_QNAME.getNamespaceURI();
   43       private static final String OLD_GENERIC_NAMESPACE = "http://geronimo.apache.org/xml/ns/web";
   44   
   45       private static final QName GENERIC_CONFIG_QNAME = new QName(GENERIC_NAMESPACE, "container-config");
   46       private static final QName OLD_GENERIC_CONFIG_QNAME = new QName(OLD_GENERIC_NAMESPACE, "container-config");
   47       private static final String SYSTEM_NAMESPACE = ModuleDocument.type.getDocumentElementName().getNamespaceURI();
   48       private static final QName SECURITY_QNAME = GerSecurityDocument.type.getDocumentElementName();
   49       private final String configNamespace;
   50       private final String namespace;
   51       private final String element;
   52       private final Set<String> excludedNamespaces = new HashSet<String>();
   53   
   54       public GenericToSpecificPlanConverter(String configNamespace, String namespace, String element) {
   55           this.configNamespace = configNamespace;
   56           this.namespace = namespace;
   57           this.element = element;
   58           excludedNamespaces.add("http://geronimo.apache.org/xml/ns/geronimo-jaspi");
   59       }
   60   
   61       public XmlObject convertToSpecificPlan(XmlObject plan) throws DeploymentException {
   62           XmlCursor rawCursor = plan.newCursor();
   63           try {
   64               if (SchemaConversionUtils.findNestedElement(rawCursor, "web-app")) {
   65                   XmlCursor temp = rawCursor.newCursor();
   66                   String namespace = temp.getName().getNamespaceURI();
   67                   temp.dispose();
   68                   if(!namespace.equals(GENERIC_NAMESPACE) && !namespace.equals(this.namespace) && !namespace.equals(OLD_GENERIC_NAMESPACE)) {
   69                       throw new DeploymentException("Cannot handle web plan with namespace "+namespace+" -- expecting "+GENERIC_NAMESPACE+" or "+this.namespace);
   70                   }
   71   
   72                   XmlObject webPlan = rawCursor.getObject().copy();
   73   
   74                   XmlCursor cursor = webPlan.newCursor();
   75                   XmlCursor end = cursor.newCursor();
   76                   try {
   77                       cursor.push();
   78                       if (cursor.toChild(GENERIC_CONFIG_QNAME) || cursor.toChild(OLD_GENERIC_CONFIG_QNAME)) {
   79                           XmlCursor source = cursor.newCursor();
   80                           cursor.push();
   81                           cursor.toEndToken();
   82                           cursor.toNextToken();
   83                           try {
   84                               if (source.toChild(configNamespace, element)) {
   85                                   source.copyXmlContents(cursor);
   86                               }
   87   
   88                           } finally {
   89                               source.dispose();
   90                           }
   91                           cursor.pop();
   92                           cursor.removeXml();
   93                       }
   94                       cursor.pop();
   95                       
   96                       cursor.push();
   97                       while (cursor.hasNextToken()) {
   98                           if (cursor.isStart()) {
   99                               if (!SchemaConversionUtils.convertSingleElementToGeronimoSubSchemas(cursor, end)
  100                               && !this.namespace.equals(cursor.getName().getNamespaceURI())
  101                                       && !excludedNamespaces.contains(cursor.getName().getNamespaceURI())) {
  102                                   cursor.setName(new QName(this.namespace, cursor.getName().getLocalPart()));
  103                               }
  104                           }
  105                           cursor.toNextToken();
  106                       }
  107                       cursor.pop();
  108                       
  109                       cursor.push();                    
  110                       Map<Object, List<XmlCursor>> map = createElementMap(cursor);
  111                       cursor.pop();
  112                                                               
  113                       moveToBottom(cursor, map.get("security-realm-name"));
  114                       moveToBottom(cursor, map.get("authentication"));
  115                       moveToBottom(cursor, map.get("security"));
  116                       moveToBottom(cursor, map.get("gbean"));
  117                       moveToBottom(cursor, map.get("persistence"));
  118                                   
  119                       clearElementMap(map);
  120                       
  121                       return webPlan;
  122                   } finally {
  123                       cursor.dispose();
  124                       end.dispose();
  125                   }
  126               } else {
  127                   throw new DeploymentException("No web-app element");
  128               }
  129           } finally {
  130               rawCursor.dispose();
  131           }
  132       }
  133                  
  134       private static Map<Object, List<XmlCursor>> createElementMap(XmlCursor cursor) {        
  135           Map<Object, List<XmlCursor>> map = new HashMap<Object, List<XmlCursor>>();   
  136           cursor.toStartDoc();
  137           if (cursor.toFirstChild()) {
  138               do {
  139                   QName name = cursor.getName();            
  140                   List<XmlCursor> locations = map.get(name);
  141                   if (locations == null) {
  142                       locations = new ArrayList<XmlCursor>();
  143                       map.put(name, locations);
  144                       map.put(name.getLocalPart(), locations);
  145                   }
  146                   locations.add(cursor.newCursor());
  147               } while(cursor.toNextSibling());
  148           }
  149           return map;
  150       }
  151       
  152       private static void clearElementMap(Map<Object, List<XmlCursor>> map) {
  153           for (Map.Entry<Object, List<XmlCursor>> entry : map.entrySet()) {
  154               for (XmlCursor cursor : entry.getValue()) {
  155                   cursor.dispose();
  156               }
  157           }
  158           map.clear();
  159       }
  160       
  161       private static void moveToBottom(XmlCursor cursor, List<XmlCursor> locations) {
  162           if (locations != null) {
  163               for (XmlCursor location : locations) {
  164                   cursor.toEndDoc();
  165                   location.moveXml(cursor);
  166               }
  167           }
  168       }
  169       
  170   }

Home » geronimo-2.2-source-release » org.apache.geronimo.web.deployment » [javadoc | source]