Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 2011-2013 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 package org.glassfish.admin.rest.generator.client;
 
 import  org.glassfish.hk2.api.ServiceLocator;
 import  org.jvnet.hk2.config.Attribute;
 import  org.jvnet.hk2.config.ConfigModel;
 import  org.jvnet.hk2.config.Dom;
 import  org.jvnet.hk2.config.DomDocument;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;

Author(s):
jasonlee
 
 public abstract class ClientGenerator {
     private static final String OUTPUT_PATH = "target/generated";
     private static final String CLIENT_PKG = "org.glassfish.admin.rest.client";
     private static final String BASE_CLASS = "org.glassfish.admin.rest.client.RestClientBase";
 
     protected Set<StringalreadyGenerated = new HashSet<String>();
     protected ServiceLocator habitat;
     protected List<Stringmessages = new ArrayList<String>();
     protected String versionString;
     protected static final String ARTIFACT_NAME = "rest-client-wrapper";
 
     private DomDocument document;
 
     public ClientGenerator(ServiceLocator habitat) {
         this. = habitat;
          = Version.getVersionNumber();
     }
 
     public abstract ClientClassWriter getClassWriter(ConfigModel modelString classNameClass parent);
     public abstract Map<StringURIgetArtifact();
 
     public List<StringgetMessages() {
         return ;
     }
 
     public void generateClasses() {
        Domain entity = getBaseServiceLocator().getService(Domain.class);
        Dom dom = Dom.unwrap(entity);
         = dom.document;
        ConfigModel rootModel = dom.document.getRoot().model;
        .clear();
        generateSingle(rootModel);
    }
    public ServiceLocator getBaseServiceLocator() {
        return ;
    }
    public void generateSingle (ConfigModel model)  {
        String className = Util.getBeanName(model.getTagName());
        if (alreadyGenerated(className)) {
            return;
        }
        ClientClassWriter writer = getClassWriter(modelclassNameRestClientBase.class);
        writer.generateGetSegment(model.getTagName());
        generateCommandMethods(writerclassName);
        Set<Stringprocessed = processElements(writermodel);
        processAttributes(writermodelprocessed);
        writer.done();
    }
    public void generateList (ClientClassWriter writer, ConfigModel model) {
        String serverConfigName = ResourceUtil.getUnqualifiedTypeName(model.targetTypeName);
        String beanName = Util.getBeanName(serverConfigName);
        generateCommandMethods(writer"List" + beanName);
//        generateGetPostCommandMethod(writer, beanName);
        generateSingle(model);
    }
    protected void generateGetPostCommandMethod(ClientClassWriter writerString resourceName) {
        String commandName = ..get("List"+resourceName);
        if (commandName != null) {
            final CommandModel cm = getCommandModel(commandName);
            if (cm != null ) {//and the command exits
                writer.generateCommandMethod(Util.methodNameFromDtdName(commandNamenull), "POST", ResourceUtil.convertToXMLName(resourceName), cm);
            }
        }
    }
    //    private void generateCommandMethods(String parentBeanName, ClassWriter parentWriter) {
    protected void generateCommandMethods(ClientClassWriter writerString className) {
        List<CommandResourceMetaDatacommandMetaData = CommandResourceMetaData.getMetaData(className);
        if (commandMetaData.size() > 0) {
            for (CommandResourceMetaData metaData : commandMetaData) {
                CommandModel cm = getCommandModel(metaData.command);
                if (cm != null) {
                    String methodName = Util.methodNameFromDtdName(metaData.commandnull);
                    if (!methodName.startsWith("_")) {
                        writer.generateCommandMethod(methodNamemetaData.httpMethodmetaData.resourcePathcm);
                    }
                }
            }
        }
    }
    protected void processAttributes(ClientClassWriter writer, ConfigModel modelSet<Stringprocessed) {
        Class clazz = model.getProxyType();
        for (Method method : clazz.getMethods()) {
            String methodName = method.getName();
            Attribute a = method.getAnnotation(Attribute.class);
            Param p = method.getAnnotation(Param.class);
            if ((a != null) || (p != null)) {
                String type = "String";
                if (a != null) {
                    type = a.dataType().getName();
                }
                if (methodName.startsWith("get") || methodName.startsWith("set")) {
                    methodName = methodName.substring(3);
                }
                String fieldName = Util.lowerCaseFirstLetter(methodName);
                if (processed.contains(fieldName)) {
                    continue;
                }
                processed.add(fieldName);
                writer.generateGettersAndSetters(typemethodNamefieldName);
            }
        }
    }
    protected String generateParameterName(ParamModel model) {
        Param param = model.getParam();
        final String paramName = (!param.alias().isEmpty()) ? param.alias() : model.getName();
        return  paramName;
    }
    protected CommandModel getCommandModel(String commandName) {
        CommandRunner cr = getBaseServiceLocator().getService(CommandRunner.class);
        return cr.getModel(commandName.);
    }
    protected Set<StringprocessElements(ClientClassWriter writer, ConfigModel model) {
        Set<Stringprocessed = new HashSet<String>();
        for (String elementName : model.getElementNames()) {
            if (processed.contains(elementName)) {
                continue;
            }
            processed.add(elementName);
            ConfigModel.Property childElement = model.getElement(elementName);
            if (elementName.equals("*")) {
                ConfigModel.Node node = (ConfigModel.Node) childElement;
                ConfigModel childModel = node.getModel();
                List<ConfigModel> subChildConfigModels = ResourceUtil.getRealChildConfigModels(childModel);
                for (ConfigModel subChildConfigModel : subChildConfigModels) {
                    if (ResourceUtil.isOnlyATag(childModel)) {
                        String childResourceClassName = ResourceUtil.getUnqualifiedTypeName(subChildConfigModel.targetTypeName);
                        writer.createGetChildResource(subChildConfigModelchildResourceClassNamechildResourceClassName);
                        generateSingle(subChildConfigModel);
                    } else {
                        processNonLeafChildConfigModel(writersubChildConfigModelchildElement);
                    }
                }
            } else  if (childElement.isLeaf()) {
                if (processed.contains(childElement.xmlName)) {
                    continue;
                }
                processed.add(childElement.xmlName);
                if (childElement.isCollection()) {
                    //generateCollectionLeafResource
                    ..println("generateCollectionLeafResource for " + elementName + " off of " + model.getTagName());
                    generateCollectionLeafResource(writerchildElement.xmlName);
                } else {
                    ..println("generateLeafResource for " + elementName + " off of " + model.getTagName());
//                    generateSingle(document.getModelByElementName(elementName));
                    generateLeafResource(writerchildElement.xmlName);
                }
            } else {
                processNonLeafChildElement(writerelementNamechildElement);
            }
        }
        return processed;
    }
    protected void generateCollectionLeafResource(ClientClassWriter writerString xmlName) {
        String className = Util.getBeanName(xmlName);
        writer.generateCollectionLeafResourceGetter(className);
        ClientClassWriter childClass = getClassWriter(nullclassNameRestLeafCollection.class);
        childClass.generateGetSegment(xmlName);
        childClass.done();
    }
    protected void generateLeafResource(ClientClassWriter writerString xmlName) {
        String className = Util.getBeanName(xmlName);
        writer.generateRestLeafGetter(className);
        ClientClassWriter childClass = getClassWriter(nullclassNameRestLeaf.class);
        childClass.generateGetSegment(xmlName);
        childClass.done();
    }
    protected void processNonLeafChildConfigModel(ClientClassWriter writer, ConfigModel childConfigModel, ConfigModel.Property childElement) {
        String childResourceClassName = ResourceUtil.getUnqualifiedTypeName(childConfigModel.targetTypeName);
        writer.createGetChildResource(childConfigModelchildResourceClassNamechildResourceClassName);
        if (childElement.isCollection()) {
            generateList(writerchildConfigModel);
        } else {
            throw new RuntimeException("The code flow should never reach here. Non-leaf ChildElements are assumed to be collection typed.");
        }
    }
    protected boolean alreadyGenerated(String className) {
        boolean retVal = true;
        if (!.contains(className)) {
            .add(className);
            retVal = false;
        }
        return retVal;
    }
    private void processNonLeafChildElement(ClientClassWriter writerString elementName, ConfigModel.Property childElement) {
        ConfigModel.Node node = (ConfigModel.Node) childElement;
        ConfigModel childModel = node.getModel();
        String beanName = ResourceUtil.getUnqualifiedTypeName(childModel.targetTypeName);
        writer.createGetChildResource(childModel, Util.upperCaseFirstLetter(Util.eleminateHypen(elementName)), beanName);
        if (childElement.isCollection()) {
            generateList(writerchildModel);
        } else {
            generateSingle(childModel);
        }
    }
New to GrepCode? Check out our FAQ X