Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.tuscany.sca.contribution.processor;
 
 import java.util.Map;
 
 
The default implementation of an extension point for StAX artifact processors.

Version:
$Rev: 830738 $ $Date: 2009-10-28 19:29:50 +0000 (Wed, 28 Oct 2009) $
 
 
     private ExtensionPointRegistry registry;
     private boolean loaded;
     private Monitor monitor = null;

    
Constructs a new extension point.
 
         this. = extensionPoints;
         this. = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
         XMLInputFactory inputFactory = .getFactory(XMLInputFactory.class);
         XMLOutputFactory outputFactory = .getFactory(XMLOutputFactory.class);
         UtilityExtensionPoint utilities = this..getExtensionPoint(UtilityExtensionPoint.class);
         MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class);
         if (monitorFactory != null) {
             this. = monitorFactory.createMonitor();
         }
         this. = new ExtensibleStAXAttributeProcessor(thisinputFactoryoutputFactory);
     }

    
Report a exception.

Parameters:
problems
message
model
 
     private void error(String messageObject modelException ex) {
         if ( != null) {
             Problem problem =
                 .createProblem(this.getClass().getName(),
                                       "contribution-validation-messages",
                                       .,
                                       model,
                                       message,
                                       ex);
             .problem(problem);
         }
     }
 
     public void addArtifactProcessor(StAXAttributeProcessor<?> artifactProcessor) {
         if (artifactProcessor.getArtifactType() != null) {
             .put((Object)artifactProcessor.getArtifactType(), artifactProcessor);
         }
         if (artifactProcessor.getModelType() != null) {
             .put(artifactProcessor.getModelType(), artifactProcessor);
        }
    }
    public void removeArtifactProcessor(StAXAttributeProcessor<?> artifactProcessor) {
        if (artifactProcessor.getArtifactType() != null) {
            .remove((Object)artifactProcessor.getArtifactType());
        }
        if (artifactProcessor.getModelType() != null) {
            .remove(artifactProcessor.getModelType());
        }
    }
    @Override
    public <T> StAXAttributeProcessor<T> getProcessor(Class<T> modelType) {
        loadArtifactProcessors();
        return (StAXAttributeProcessor<T>)super.getProcessor(modelType);
    }
    @Override
    public StAXAttributeProcessor<?> getProcessor(Object artifactType) {
        loadArtifactProcessors();
        return super.getProcessor(artifactType);
    }

    
Lazily load artifact processors registered in the extension point.
    private synchronized void loadArtifactProcessors() {
        if ()
            return;
        // Get the processor service declarations
        Collection<ServiceDeclarationprocessorDeclarations;
        try {
            processorDeclarations =
        } catch (IOException e) {
            IllegalStateException ie = new IllegalStateException(e);
            error("IllegalStateException"ie);
            throw ie;
        }
        for (ServiceDeclaration processorDeclaration : processorDeclarations) {
            Map<StringStringattributes = processorDeclaration.getAttributes();
            // Load a StAX artifact processor
            // Get the model QName
            QName artifactType = ServiceDeclarationParser.getQName(attributes.get("qname"));
            // Get the model class name
            String modelTypeName = attributes.get("model");
            // Get the model factory class name
            String factoryName = attributes.get("factory");
            // Create a processor wrapper and register it
            StAXAttributeProcessor<?> processor =
                new LazyStAXAttributeProcessor(artifactTypemodelTypeNamefactoryNameprocessorDeclaration,
                                               );
            addArtifactProcessor(processor);
        }
         = true;
    }

    
A wrapper around an Artifact processor class allowing lazy loading and initialization of artifact processors.
    private static class LazyStAXAttributeProcessor implements StAXAttributeProcessor {
        private ExtensionPointRegistry extensionPoints;
        private QName artifactType;
        private String modelTypeName;
        private String factoryName;
        private ServiceDeclaration processorDeclaration;
        private StAXAttributeProcessor<?> processor;
        private Class<?> modelType;
        LazyStAXAttributeProcessor(QName artifactType,
                                   String modelTypeName,
                                   String factoryName,
                                   ServiceDeclaration processorDeclaration,
                                   ExtensionPointRegistry extensionPoints,
                                   FactoryExtensionPoint modelFactories,
                                   StAXAttributeProcessor<ObjectextensionProcessor) {
            this. = extensionPoints;
            this. = artifactType;
            this. = modelTypeName;
            this. = factoryName;
            this. = processorDeclaration;
            this. = extensionProcessor;
        }
        public QName getArtifactType() {
            return ;
        }
        @SuppressWarnings("unchecked")
        private StAXAttributeProcessor getProcessor() {
            if ( == null) {
                FactoryExtensionPoint modelFactories = .getExtensionPoint(FactoryExtensionPoint.class);
                // Load and instantiate the processor class
                try {
                    Class<StAXAttributeProcessorprocessorClass =
                        (Class<StAXAttributeProcessor>).loadClass();
                    try {
                        Constructor<StAXAttributeProcessorconstructor =
                            processorClass.getConstructor(FactoryExtensionPoint.class);
                         = constructor.newInstance(modelFactories);
                    } catch (NoSuchMethodException e) {
                        try {
                            Constructor<StAXAttributeProcessorconstructor =
                                processorClass.getConstructor(ExtensionPointRegistry.class);
                             = constructor.newInstance();
                        } catch (NoSuchMethodException e1) {
                            try {
                                Constructor<StAXAttributeProcessorconstructor =
                                    processorClass.getConstructor(FactoryExtensionPoint.class,
                                                                  StAXArtifactProcessor.class);
                                 = constructor.newInstance(modelFactories);
                            } catch (NoSuchMethodException e2) {
                                try {
                                    Constructor<StAXAttributeProcessorconstructor =
                                        processorClass.getConstructor(ExtensionPointRegistry.class,
                                                                      StAXArtifactProcessor.class);
                                     = constructor.newInstance();
                                } catch (NoSuchMethodException e3) {
                                    try {
                                        Constructor<StAXAttributeProcessorconstructor =
                                            processorClass.getConstructor(FactoryExtensionPoint.class);
                                         = constructor.newInstance(modelFactories);
                                    } catch (NoSuchMethodException e4) {
                                        try {
                                            Constructor<StAXAttributeProcessorconstructor =
                                                processorClass.getConstructor(ExtensionPointRegistry.class);
                                             = constructor.newInstance();
                                        } catch (NoSuchMethodException e4a) {
                                            Constructor<StAXAttributeProcessorconstructor =
                                                processorClass.getConstructor();
                                             = constructor.newInstance();
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    IllegalStateException ie = new IllegalStateException(e);
                    throw ie;
                }
            }
            return ;
        }
        public Object read(QName attributeNameXMLStreamReader inputSourceProcessorContext context)
            throws ContributionReadExceptionXMLStreamException {
            return getProcessor().read(attributeNameinputSourcecontext);
        }
        @SuppressWarnings("unchecked")
        public void write(Object modelXMLStreamWriter outputSourceProcessorContext context)
            throws ContributionWriteExceptionXMLStreamException {
            getProcessor().write(modeloutputSourcecontext);
        }
        public Class<?> getModelType() {
            if ( != null &&  == null) {
                try {
                     = .loadClass();
                } catch (Exception e) {
                    IllegalStateException ie = new IllegalStateException(e);
                    throw ie;
                }
            }
            return ;
        }
        @SuppressWarnings("unchecked")
        public void resolve(Object modelModelResolver resolverProcessorContext context)
            throws ContributionResolveException {
            getProcessor().resolve(modelresolvercontext);
        }
    }
New to GrepCode? Check out our FAQ X