Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.core.jaxb » [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.st.core.jaxb;
   18   
   19   import javax.xml.bind.Marshaller;
   20   
   21   import org.apache.geronimo.jee.naming.GbeanLocator;
   22   import org.apache.geronimo.jee.naming.Pattern;
   23   import org.apache.geronimo.jee.naming.PersistenceContextRef;
   24   import org.apache.geronimo.jee.naming.ResourceLocator;
   25   import org.apache.geronimo.jee.openejb.EjbRelationshipRole;
   26   import org.apache.geronimo.jee.openejb.OpenejbJar;
   27   import org.apache.geronimo.jee.openejb.Relationships;
   28   import org.apache.geronimo.jee.security.Security;
   29   import org.apache.geronimo.jee.web.WebApp;
   30   
   31   public class MarshallerListener extends Marshaller.Listener{
   32   
   33       @Override
   34       public void beforeMarshal(Object source) {
   35           if (source instanceof Security) {
   36               Security security = (Security)source;
   37               if (security.getRoleMappings() != null && security.getRoleMappings().getRole().size() == 0) {
   38                   security.setRoleMappings(null);
   39               } 
   40           } else if (source instanceof WebApp) {
   41               WebApp webapp = (WebApp)source;
   42               GbeanLocator gbeanlocator = webapp.getWebContainer();
   43               if (gbeanlocator != null && isEmpty(gbeanlocator.getGbeanLink()) && isEmpty(gbeanlocator.getPattern())) {
   44                   webapp.setWebContainer(null);
   45               }
   46           } else if (source instanceof OpenejbJar) {
   47               OpenejbJar openejb = (OpenejbJar)source;
   48               ResourceLocator locator = openejb.getCmpConnectionFactory();
   49               if (locator != null && isEmpty(locator.getResourceLink()) && isEmpty(locator.getUrl()) && isEmpty(locator.getPattern())) {
   50                   openejb.setCmpConnectionFactory(null);
   51               }
   52               Relationships relationships = openejb.getRelationships();
   53               if (relationships != null && relationships.getEjbRelation().size() == 0) {
   54                   openejb.setRelationships(null);
   55               }
   56           } else if (source instanceof PersistenceContextRef) {
   57               PersistenceContextRef contextRef = (PersistenceContextRef)source;
   58               if (contextRef.getPattern() != null && isEmpty(contextRef.getPattern())) {
   59                   contextRef.setPattern(null);
   60               }
   61           } else if (source instanceof EjbRelationshipRole) {
   62               EjbRelationshipRole role = (EjbRelationshipRole)source;
   63               if (role.getRoleMapping() != null && role.getRoleMapping().getCmrFieldMapping().size() == 0) {
   64                   role.setRoleMapping(null);
   65               }
   66           }
   67       }
   68           
   69       private boolean isEmpty(Pattern pattern) {
   70           if ( pattern == null ) {
   71               return true;
   72           }
   73           if ( isEmpty(pattern.getGroupId()) && isEmpty(pattern.getArtifactId()) &&
   74                   isEmpty(pattern.getModule()) && isEmpty(pattern.getName()) &&
   75                   isEmpty(pattern.getVersion()) ) {
   76               return true;
   77           }
   78           return false;
   79       }
   80       
   81       private boolean isEmpty(String value) {
   82           
   83           return (value == null || value.trim().equals(""));
   84       }
   85   
   86   }

Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.core.jaxb » [javadoc | source]