org/objectweb/proactive/core/mop/ConstructorCallImpl.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.mop;
00032 
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.lang.reflect.Constructor;
00036 import java.lang.reflect.InvocationTargetException;
00037 import java.lang.reflect.Modifier;
00038 
00039 
00043 public class ConstructorCallImpl implements ConstructorCall, Serializable {
00044 
00048     public Object[] effectiveArguments;
00049 
00053     public Constructor reifiedConstructor;
00054 
00055     //
00056     // -- CONSTRUCTORS -----------------------------------------------
00057     //
00058 
00064     public ConstructorCallImpl(Constructor reifiedConstructor,
00065         Object[] effectiveArguments) {
00066         this.reifiedConstructor = reifiedConstructor;
00067         this.effectiveArguments = effectiveArguments;
00068     }
00069 
00070     //
00071     // -- PUBLIC METHODS -----------------------------------------------
00072     //
00073     public String toString() {
00074         StringBuffer sb = new StringBuffer();
00075         sb.append("ConstructorCallImpl\n");
00076         sb.append("reifiedConstructor=");
00077         sb.append(reifiedConstructor);
00078         sb.append("\n");
00079         sb.append("effectiveArguments=");
00080         if (effectiveArguments == null) {
00081             sb.append("null\n");
00082         } else {
00083             sb.append("\n");
00084             for (int i = 0; i < effectiveArguments.length; i++) {
00085                 sb.append("   effectiveArguments[");
00086                 sb.append(i);
00087                 sb.append("]=");
00088                 sb.append(effectiveArguments[i]);
00089                 sb.append("\n");
00090             }
00091             sb.append("\n");
00092         }
00093         return sb.toString();
00094     }
00095 
00096     //
00097     // -- implements ConstructorCall -----------------------------------------------
00098     //
00099 
00103     public void makeDeepCopyOfArguments() throws java.io.IOException {
00104         effectiveArguments = (Object[]) Utils.makeDeepCopy(effectiveArguments);
00105     }
00106 
00110     public String getTargetClassName() {
00111         return getReifiedClass().getName();
00112     }
00113 
00119     public Object execute()
00120         throws InvocationTargetException, 
00121             ConstructorCallExecutionFailedException {
00122         // System.out.println("ConstructorCall: The constructor is " + reifiedConstructor); 
00123         try {
00124             return reifiedConstructor.newInstance(effectiveArguments);
00125         } catch (IllegalAccessException e) {
00126             throw new ConstructorCallExecutionFailedException(
00127                 "Access rights to the constructor denied: " + e);
00128         } catch (IllegalArgumentException e) {
00129             throw new ConstructorCallExecutionFailedException(
00130                 "Illegal constructor arguments: " + e);
00131         } catch (InstantiationException e) {
00132             if (getReifiedClass().isInterface()) {
00133                 throw new ConstructorCallExecutionFailedException(
00134                     "Cannot build an instance of an interface: " + e);
00135             } else if (Modifier.isAbstract(getReifiedClass().getModifiers())) {
00136                 throw new ConstructorCallExecutionFailedException(
00137                     "Cannot build an instance of an abstract class: " + e);
00138             } else {
00139                 throw new ConstructorCallExecutionFailedException(
00140                     "Instanciation problem: " + e +
00141                     ". Strange enough, the reified class is neither abstract nor an interface.");
00142             }
00143         } catch (ExceptionInInitializerError e) {
00144             throw new ConstructorCallExecutionFailedException(
00145                 "Cannot build object because the initialization of its class failed: " +
00146                 e);
00147         }
00148     }
00149 
00150     //
00151     // -- PROTECTED METHODS -----------------------------------------------
00152     //
00153 
00158     protected Class getReifiedClass() {
00159         return reifiedConstructor.getDeclaringClass();
00160     }
00161 
00162     //
00163     // -- PRIVATE METHODS -----------------------------------------------
00164     //
00165     private void writeObject(java.io.ObjectOutputStream out)
00166         throws IOException {
00167         // We want to implement a workaround the Constructor
00168         // not being Serializable
00169         out.writeObject(this.effectiveArguments);
00170         // Constructor needs to be converted because it is not serializable
00171         Class declaringClass;
00172         Class[] parameters;
00173 
00174         declaringClass = this.reifiedConstructor.getDeclaringClass();
00175         out.writeObject(declaringClass);
00176 
00177         parameters = this.reifiedConstructor.getParameterTypes();
00178         out.writeObject(parameters);
00179     }
00180 
00181     private void readObject(java.io.ObjectInputStream in)
00182         throws IOException, ClassNotFoundException {
00183         Class declaringClass = null;
00184         Class[] parameters;
00185         try {
00186             this.effectiveArguments = (Object[]) in.readObject();
00187         } catch (IOException e) {
00188             //  System.out.println("Stream is  " + in.getClass().getName());
00189             //    e.printStackTrace();
00190             throw e;
00191         }
00192 
00193         declaringClass = (Class) in.readObject();
00194         parameters = (Class[]) in.readObject();
00195 
00196         try {
00197             this.reifiedConstructor = declaringClass.getConstructor(parameters);
00198         } catch (NoSuchMethodException e) {
00199             throw new InternalException("Lookup for constructor failed: " + e +
00200                 ". This may be caused by having different versions of the same class on different VMs. Check your CLASSPATH settings.");
00201         }
00202     }
00203 }

Generated on Mon Jan 22 15:16:08 2007 for ProActive by  doxygen 1.5.1