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.databinding;
 
 import java.util.List;
 import java.util.Map;
 

Version:
$Rev: 830738 $ $Date: 2009-10-28 19:29:50 +0000 (Wed, 28 Oct 2009) $
 
     private static final Logger logger = Logger.getLogger(DefaultTransformerExtensionPoint.class.getName());
     private boolean loadedTransformers;
 
     private ExtensionPointRegistry registry;
     private final DirectedGraph<ObjectTransformergraph = new DirectedGraph<ObjectTransformer>();
 
         this. = registry;
     }
 
     public void addTransformer(String sourceTypeString resultTypeint weightTransformer transformerboolean publicTransformer) {
         if (.isLoggable(.)) {
             String className = transformer.getClass().getName();
             boolean lazy = false;
             boolean pull = (transformer instanceof PullTransformer);
             if (transformer instanceof LazyPullTransformer) {
                 className = ((LazyPullTransformer)transformer)..getClassName();
                 lazy = true;
             }
             if (transformer instanceof LazyPushTransformer) {
                 className = ((LazyPushTransformer)transformer)..getClassName();
                 lazy = true;
             }
 
             .fine("Adding transformer: " + className
                 + ";source="
                 + sourceType
                 + ",target="
                 + resultType
                 + ",weight="
                 + weight
                 + ",type="
                 + (pull ? "pull" : "push")
                 + ",lazy="
                 + lazy);
         }
         .addEdge(sourceTyperesultTypetransformerweightpublicTransformer);
     }
 
     public void addTransformer(Transformer transformerboolean publicTransformer) {
         addTransformer(transformer.getSourceDataBinding(),
                        transformer.getTargetDataBinding(),
                        transformer.getWeight(),
                        transformerpublicTransformer);
     }
 
     public boolean removeTransformer(String sourceTypeString resultType) {
         return .removeEdge(sourceTyperesultType);
     }
 
     public Transformer getTransformer(String sourceTypeString resultType) {
         loadTransformers();
 
         DirectedGraph<ObjectTransformer>.Edge edge = .getEdge(sourceTyperesultType);
         return (edge == null) ? null : edge.getValue();
     }

    
Dynamically load transformers registered under META-INF/services.
 
    private synchronized void loadTransformers() {
        if () {
            return;
        }
         = true;
        loadTransformers(PullTransformer.class);
        loadTransformers(PushTransformer.class);
    }

    
Dynamically load transformers registered under META-INF/services.

Parameters:
transformerClass
    private synchronized void loadTransformers(Class<?> transformerClass) {
        // Get the transformer service declarations
        Collection<ServiceDeclarationtransformerDeclarations;
        try {
            transformerDeclarations = .getServiceDiscovery().getServiceDeclarations(transformerClass.getName());
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        // Load transformers
        for (ServiceDeclaration transformerDeclaration : transformerDeclarations) {
            Map<StringStringattributes = transformerDeclaration.getAttributes();
            String source = attributes.get("source");
            String target = attributes.get("target");
            int weight = Integer.valueOf(attributes.get("weight"));
            String b = attributes.get("public");
            boolean pub = true;
            if (b != null) {
                pub = Boolean.valueOf(b);
            }
            // Create a transformer wrapper and register it
            Transformer transformer;
            if (transformerClass == PullTransformer.class) {
                transformer = new LazyPullTransformer(sourcetargetweighttransformerDeclaration);
            } else {
                transformer = new LazyPushTransformer(sourcetargetweighttransformerDeclaration);
            }
            addTransformer(transformerpub);
        }
    }

    
A transformer facade allowing transformers to be lazily loaded and initialized.
    private class LazyPullTransformer implements PullTransformer<ObjectObject> {
        private String source;
        private String target;
        private int weight;
        private PullTransformer<ObjectObjecttransformer;
        public LazyPullTransformer(String sourceString targetint weightServiceDeclaration transformerDeclaration) {
            this. = source;
            this. = target;
            this. = weight;
            this. = transformerDeclaration;
        }

        
Load and instantiate the transformer class.

Returns:
The transformer.
        @SuppressWarnings("unchecked")
        private PullTransformer<ObjectObjectgetTransformer() {
            if ( == null) {
                try {
                    Class<PullTransformer<ObjectObject>> transformerClass =
                        (Class<PullTransformer<ObjectObject>>).loadClass();
                    try {
                        Constructor<PullTransformer<ObjectObject>> constructor = transformerClass.getConstructor();
                         = constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<PullTransformer<ObjectObject>> constructor =
                            transformerClass.getConstructor(ExtensionPointRegistry.class);
                         = constructor.newInstance();
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
        public String getSourceDataBinding() {
            return ;
        }
        public String getTargetDataBinding() {
            return ;
        }
        public int getWeight() {
            return ;
        }
        public Object transform(Object sourceTransformationContext context) {
            return getTransformer().transform(sourcecontext);
        }
        @Override
        public String toString() {
            StringBuffer sb = new StringBuffer(super.toString());
            sb.append(";className=").append(.getClassName());
            return sb.toString();
        }
    }

    
A transformer facade allowing transformers to be lazily loaded and initialized.
    private class LazyPushTransformer implements PushTransformer<ObjectObject> {
        private String source;
        private String target;
        private int weight;
        private PushTransformer<ObjectObjecttransformer;
        public LazyPushTransformer(String sourceString targetint weightServiceDeclaration transformerDeclaration) {
            this. = source;
            this. = target;
            this. = weight;
            this. = transformerDeclaration;
        }

        
Load and instantiate the transformer class.

Returns:
The transformer.
        @SuppressWarnings("unchecked")
        private PushTransformer<ObjectObjectgetTransformer() {
            if ( == null) {
                try {
                    Class<PushTransformer<ObjectObject>> transformerClass =
                        (Class<PushTransformer<ObjectObject>>).loadClass();
                    try {
                        Constructor<PushTransformer<ObjectObject>> constructor = transformerClass.getConstructor();
                         = constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<PushTransformer<ObjectObject>> constructor =
                            transformerClass.getConstructor(ExtensionPointRegistry.class);
                         = constructor.newInstance();
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
        public String getSourceDataBinding() {
            return ;
        }
        public String getTargetDataBinding() {
            return ;
        }
        public int getWeight() {
            return ;
        }
        public void transform(Object sourceObject sinkTransformationContext context) {
            getTransformer().transform(sourcesinkcontext);
        }
        @Override
        public String toString() {
            StringBuffer sb = new StringBuffer(super.toString());
            sb.append(";className=").append(.getClassName());
            return sb.toString();
        }
    }
    //FIXME The following methods should be on a different class from
    // extension point
    public List<TransformergetTransformerChain(String sourceTypeString resultType) {
        loadTransformers();
        String source = sourceType;
        String result = resultType;
        List<Transformertransformers = new ArrayList<Transformer>();
        // First check if there is a direct path, if yes, use it regardless of the weight
        DirectedGraph<ObjectTransformer>.Edge link = .getEdge(sourceTyperesultType);
        if (link != null) {
            transformers.add(link.getValue());
        } else {
            DirectedGraph<ObjectTransformer>.Path path = .getShortestPath(sourceresult);
            if (path == null) {
                return null;
            }
            for (DirectedGraph<ObjectTransformer>.Edge edge : path.getEdges()) {
                transformers.add(edge.getValue());
            }
        }
        return transformers;
    }
    @Override
    public String toString() {
        loadTransformers();
        return .toString();
    }
New to GrepCode? Check out our FAQ X