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;
 
The default implementation of a data binding extension point.

Version:
$Rev: 933855 $ $Date: 2010-04-14 05:16:49 +0100 (Wed, 14 Apr 2010) $
 
     private ExtensionPointRegistry registry;
     private final Map<StringDataBindingbindings = new HashMap<StringDataBinding>();
     private final List<DataBindingdatabindings = new ArrayList<DataBinding>();
     private static final Logger logger = Logger.getLogger(DefaultDataBindingExtensionPoint.class.getName());
     private boolean loadedDataBindings;
 
 //    public DefaultDataBindingExtensionPoint() {
 //    }
 
         this. = registry;
     }
 
     public DataBinding getDataBinding(String id) {
         if (id == null) {
             return null;
         }
         loadDataBindings();
         DataBinding dataBinding = .get(id.toLowerCase());
         if (dataBinding == null) {
             loadDataBindings();
             dataBinding = .get(id.toLowerCase());
         }
         return dataBinding;
     }
 
     public void addDataBinding(DataBinding dataBinding) {
         if (.isLoggable(.)) {
             String className = dataBinding.getClass().getName();
             boolean lazy = false;
             if (dataBinding instanceof LazyDataBinding) {
                 className = ((LazyDataBinding)dataBinding)..getClassName();
                 lazy = true;
             }
             .fine("Adding databinding: " + className + ";name=" + dataBinding.getName() + ",lazy=" + lazy);
         }
         .add(dataBinding);
         .put(dataBinding.getName().toLowerCase(), dataBinding);
 
     }
 
     public DataBinding removeDataBinding(String id) {
         if (id == null) {
             return null;
         }
         DataBinding dataBinding = .remove(id.toLowerCase());
         if (dataBinding != null) {
             .remove(dataBinding);
         }
         return dataBinding;
     }

    
Dynamically load data bindings declared under META-INF/services
    private synchronized void loadDataBindings() {
        if ()
            return;
        // Get the databinding service declarations
        Collection<ServiceDeclarationdataBindingDeclarations;
        try {
            dataBindingDeclarations = .getServiceDiscovery().getServiceDeclarations(DataBinding.class.getName());
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        // Load data bindings
        for (ServiceDeclaration dataBindingDeclaration : dataBindingDeclarations) {
            Map<StringStringattributes = dataBindingDeclaration.getAttributes();
            String name = attributes.get("name");
            // Create a data binding wrapper and register it
            DataBinding dataBinding = new LazyDataBinding(namedataBindingDeclaration);
            addDataBinding(dataBinding);
        }
         = true;
    }

    
A data binding facade allowing data bindings to be lazily loaded and initialized.
    private class LazyDataBinding implements DataBinding {
        private String name;
        private DataBinding dataBinding;
        private LazyDataBinding(String typeServiceDeclaration dataBindingDeclaration) {
            this. = type;
            this. = dataBindingDeclaration;
        }

        
Load and instantiate the data binding class.

Returns:
The data binding.
        @SuppressWarnings("unchecked")
        private DataBinding getDataBinding() {
            if ( == null) {
                try {
                    Class<DataBindingdataBindingClass = (Class<DataBinding>).loadClass();
                    try {
                        Constructor<DataBindingconstructor = dataBindingClass.getConstructor();
                         = constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<DataBindingconstructor =
                            dataBindingClass.getConstructor(ExtensionPointRegistry.class);
                         = constructor.newInstance(DefaultDataBindingExtensionPoint.this.);
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
        public Object copy(Object objectDataType sourceDataTypeDataType targetDataTypeOperation sourceOperationOperation targetOperation) {
            return getDataBinding().copy(objectsourceDataTypetargetDataTypesourceOperationtargetOperation);
        }
        public String getName() {
            return ;
        }
        public XMLTypeHelper getXMLTypeHelper() {
            return getDataBinding().getXMLTypeHelper();
        }
        public WrapperHandler getWrapperHandler() {
            return getDataBinding().getWrapperHandler();
        }
        public boolean introspect(DataType dataTypeOperation operation) {
            return getDataBinding().introspect(dataTypeoperation);
        }
        public DataType introspect(Object valueOperation operation) {
            return getDataBinding().introspect(valueoperation);
        }
    }
    //FIXME The following methods should not be on the extension point
    // they should be on a separate class
    public boolean introspectType(DataType dataTypeOperation operation) {
        loadDataBindings();
        for (DataBinding binding : ) {
            // don't introspect for JavaBeansDatabinding as all javatypes will
            // anyways match to its basetype
            // which is java.lang.Object. Default to this only if no databinding
            // results
            if (!binding.getName().equals(.)) {
                if (binding.introspect(dataTypeoperation)) {
                    return true;
                }
            }
        }
        // FIXME: Should we honor the databinding from operation/interface
        // level?
        Class<?> physical = dataType.getPhysical();
        if (physical == Object.class) {
            dataType.setDataBinding(.);
            return false;
        }
        if (dataType.getPhysical().isArray()) {
            introspectArray(dataTypeoperation);
            return true;
        } else if (Throwable.class.isAssignableFrom(physical)) {
            dataType.setDataBinding(.);
            return true;
        } else {
            dataType.setDataBinding(.);
            return false;
        }
    }
    private boolean introspectArray(DataType dataTypeOperation operation) {
        Class<?> physical = dataType.getPhysical();
        if (!physical.isArray() || physical == byte[].class) {
            return false;
        }
        Class<?> componentType = physical.getComponentType();
        Type genericComponentType = componentType;
        if(dataType.getGenericType() instanceof GenericArrayType) {
            genericComponentType = ((GenericArrayTypedataType.getGenericType()).getGenericComponentType();
        }
        DataType logical = new DataTypeImpl(dataType.getDataBinding(), componentTypegenericComponentTypedataType.getLogical());
        introspectType(logicaloperation);
        dataType.setDataBinding("java:array");
        dataType.setLogical(logical);
        return true;
    }
    public DataType introspectType(Object valueOperation operation) {
        loadDataBindings();
        DataType dataType = null;
        for (DataBinding binding : ) {
            // don't introspect for JavaBeansDatabinding as all javatypes will
            // anyways match to its basetype
            // which is java.lang.Object. Default to this only if no databinding
            // results
            if (!binding.getName().equals(.)) {
                dataType = binding.introspect(valueoperation);
            }
            if (dataType != null) {
                return dataType;
            }
        }
        return new DataTypeImpl<XMLType>(.value.getClass(), .);
    }
New to GrepCode? Check out our FAQ X