1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.services.adaption.reflection;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.Arrays;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.adaption.Adapter;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.procedure.AsyncProcedure;
26 public class ReflectionAdapter2<T> implements Adapter<T, Resource> {
28 Constructor<? extends T> constructor;
29 IDynamicAdapter2[] parameters;
31 public ReflectionAdapter2(Class<? extends T> clazz, IDynamicAdapter2 ... parameters) throws SecurityException, NoSuchMethodException, DatabaseException {
32 Class<?>[] parameterTypes = new Class<?>[parameters.length];
33 for(int i=0;i<parameters.length;++i)
34 parameterTypes[i] = parameters[i].getType();
35 this.constructor = clazz.getConstructor(parameterTypes);
36 this.parameters = parameters;
40 public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {
42 if(parameters.length == 0) {
44 // System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);
47 procedure.execute(g, constructor.newInstance());
48 } catch (IllegalArgumentException e) {
49 procedure.exception(g, e);
51 } catch (InstantiationException e) {
52 procedure.exception(g, e);
54 } catch (IllegalAccessException e) {
55 procedure.exception(g, e);
57 } catch (InvocationTargetException e) {
58 procedure.exception(g, e.getCause());
59 e.getCause().printStackTrace();
62 } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {
65 procedure.execute(g, constructor.newInstance(r));
66 } catch (IllegalArgumentException e) {
67 procedure.exception(g, e);
69 } catch (InstantiationException e) {
70 procedure.exception(g, e);
72 } catch (IllegalAccessException e) {
73 procedure.exception(g, e);
75 } catch (InvocationTargetException e) {
76 procedure.exception(g, e.getCause());
77 e.getCause().printStackTrace();
82 g.asyncRequest(new ReadRequest() {
85 public void run(ReadGraph graph) throws DatabaseException {
87 Object[] args = new Object[parameters.length];
89 for(int i=0;i<parameters.length;++i)
90 args[i] = parameters[i].adapt(graph, r);
91 procedure.execute(graph, constructor.newInstance(args));
92 } catch (IllegalArgumentException e) {
93 procedure.exception(graph, e);
95 } catch (InstantiationException e) {
96 procedure.exception(graph, e);
98 } catch (IllegalAccessException e) {
99 procedure.exception(graph, e);
101 } catch (InvocationTargetException e) {
102 procedure.exception(graph, e.getCause());
103 e.getCause().printStackTrace();
104 } catch (DatabaseException e) {
105 procedure.exception(graph, e);
107 } catch (Throwable t) {
108 procedure.exception(graph, t);
114 public String toString() {
115 return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);
125 public String toString() {
126 return "ReflectionAdapter for " + constructor.getName();