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.javabeans;
 
 
DataBinding for JavaBeans

Version:
$Rev: 916888 $ $Date: 2010-02-27 00:44:05 +0000 (Sat, 27 Feb 2010) $
 
 public class JavaBeansDataBinding extends BaseDataBinding {
     private final static Logger logger = Logger.getLogger(JavaBeansDataBinding.class.getName());
    
Defining a weight to a very high number so that the transformer won't be picked up by other paths unless it's the only available path
 
     public static final int HEAVY_WEIGHT = 10000;
     public static final String NAME = "java:complexType";
 
     public JavaBeansDataBinding() {
         super(Object.class);
     }
 
     protected JavaBeansDataBinding(String nameClass<?> baseType) {
         super(namebaseType);
     }
 
     @Override
     public Object copy(Object argDataType sourceDataTypeDataType targetDataTypeOperation sourceOperationOperation targetOperation) {
         if (arg == null) {
             return null;
         }
         final Class<?> clazz = arg.getClass();
         if (String.class == clazz || clazz.isPrimitive()
             || Number.class.isAssignableFrom(clazz)
             || Boolean.class.isAssignableFrom(clazz)
             || Character.class.isAssignableFrom(clazz)
             || Byte.class.isAssignableFrom(clazz)) {
             // Immutable classes
             return arg;
         }
         try {
             if (arg instanceof Serializable) {
                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
                 ObjectOutputStream oos = getObjectOutputStream(bos);
                 oos.writeObject(arg);
                 oos.close();
                 bos.close();
 
                 // Work out which ClassLoader to use for deserializing arg
                 // We want to use:
                 //   * The ClassLoader of the targetDataType if it is not the System ClassLoader
                 //   * The ClassLoader of arg if it is not the System ClassLoader
                 //   * The ThreadContext ClassLoader if the ClassLoader of arg is the System ClassLoader
                 //     because Collection classes are loaded by the System ClassLoader but their contents
                 //     may be loaded from another ClassLoader
                 //
                 ClassLoader classLoaderToUse = targetDataType.getPhysical().getClassLoader();
                 if (classLoaderToUse == null) {
                     classLoaderToUse = clazz.getClassLoader();
                 }
                 if (classLoaderToUse == null)
                 {
                     // ClassLoader of arg is the System ClassLoader so we will use the ThreadContext ClassLoader
                     // instead
                     classLoaderToUse = Thread.currentThread().getContextClassLoader();
                }
                ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
                ObjectInputStream ois = getObjectInputStream(bisclassLoaderToUse);
                Object objectCopy = ois.readObject();
                ois.close();
                bis.close();
                return objectCopy;
            } else if (arg instanceof Cloneable) {
                Method clone;
                try {
                    clone = arg.getClass().getMethod("clone");
                    try {
                        return clone.invoke(arg, (Object[])null);
                    } catch (InvocationTargetException e) {
                        if (e.getTargetException() instanceof CloneNotSupportedException) {
                            // Ignore
                        } else {
                            throw new IllegalArgumentException(e);
                        }
                    } catch (Exception e) {
                        throw new IllegalArgumentException(e);
                    }
                } catch (NoSuchMethodException e) {
                    // Ignore it
                }
            }
                // return arg;
            .warning("Argument type '" + arg.getClass().getName()
                + "' is not Serializable or Cloneable. Pass-by-value is skipped.");
            return arg;
       } catch (Exception e) {
            throw new IllegalArgumentException("Pass-by-value is not supported for the given object: " + arg.getClass()
                .getName(), e);
        }
    }
        return new ObjectOutputStream(os);
    }
    protected ObjectInputStream getObjectInputStream(InputStream isfinal ClassLoader clthrows IOException {
        ObjectInputStream ois = new ObjectInputStream(is) {
            @Override
            protected Class<?> resolveClass(ObjectStreamClass descthrows IOExceptionClassNotFoundException {
                try {
                    return Class.forName(desc.getName(), falsecl);
                } catch (ClassNotFoundException e) {
                    try {
                        // For OSGi, use context ClassLoader if the bundle ClassLoader cannot load the class
                        if (cl != Thread.currentThread().getContextClassLoader()) {
                            return Class.forName(desc.getName(), false, Thread.currentThread().getContextClassLoader());
                        }
                    } catch (ClassNotFoundException e1) {
                        // ignore
                    } catch (NoClassDefFoundError e1) {
                        // ignore
                    }
                    return super.resolveClass(desc);
                }
            }
        };
        return ois;
    }
New to GrepCode? Check out our FAQ X