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.adaption.Adapter;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.AsyncProcedure;
23 import org.simantics.db.request.AsyncRead;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class ReflectionAdapter2<T> implements Adapter<T, Resource> {
29 private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionAdapter2.class);
31 Constructor<? extends T> constructor;
32 IDynamicAdapter2[] parameters;
34 public ReflectionAdapter2(Class<? extends T> clazz, IDynamicAdapter2 ... parameters) throws SecurityException, NoSuchMethodException, DatabaseException {
35 Class<?>[] parameterTypes = new Class<?>[parameters.length];
36 for(int i=0;i<parameters.length;++i)
37 parameterTypes[i] = parameters[i].getType();
38 this.constructor = clazz.getConstructor(parameterTypes);
39 this.parameters = parameters;
43 public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {
45 if(parameters.length == 0) {
48 procedure.execute(g, constructor.newInstance());
49 } catch (IllegalArgumentException e) {
50 procedure.exception(g, e);
51 LOGGER.error("Unexpected exception during adapter creation", e);
52 } catch (InstantiationException e) {
53 procedure.exception(g, e);
54 LOGGER.error("Unexpected exception during adapter creation", e);
55 } catch (IllegalAccessException e) {
56 procedure.exception(g, e);
57 LOGGER.error("Unexpected exception during adapter creation", e);
58 } catch (InvocationTargetException e) {
59 procedure.exception(g, e.getCause());
60 LOGGER.error("Unexpected exception during adapter creation", e.getCause());
63 } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {
66 procedure.execute(g, constructor.newInstance(r));
67 } catch (IllegalArgumentException e) {
68 procedure.exception(g, e);
69 LOGGER.error("Unexpected exception during adapter creation", e);
70 } catch (InstantiationException e) {
71 procedure.exception(g, e);
72 LOGGER.error("Unexpected exception during adapter creation", e);
73 } catch (IllegalAccessException e) {
74 procedure.exception(g, e);
75 LOGGER.error("Unexpected exception during adapter creation", e);
76 } catch (InvocationTargetException e) {
77 procedure.exception(g, e.getCause());
78 LOGGER.error("Unexpected exception during adapter creation", e.getCause());
83 g.asyncRequest(new AsyncRead<T>() {
86 public void perform(AsyncReadGraph graph, AsyncProcedure<T> p) {
88 Object[] args = new Object[parameters.length];
90 for(int i=0;i<parameters.length;++i)
91 args[i] = parameters[i].adapt(graph, r);
92 p.execute(graph, constructor.newInstance(args));
93 } catch (IllegalArgumentException e) {
95 LOGGER.error("Unexpected exception during adapter creation", e);
96 } catch (InstantiationException e) {
98 LOGGER.error("Unexpected exception during adapter creation", e);
99 } catch (IllegalAccessException e) {
101 LOGGER.error("Unexpected exception during adapter creation", e);
102 } catch (InvocationTargetException e) {
103 p.exception(g, e.getCause());
104 LOGGER.error("Unexpected exception during adapter creation", e);
105 } catch (DatabaseException e) {
107 LOGGER.error("Unexpected exception during adapter creation", e);
108 } catch (Throwable t) {
110 LOGGER.error("Unexpected exception during adapter creation", t);
116 public String toString() {
117 return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);
127 public String toString() {
128 return "ReflectionAdapter for " + constructor.getName();