]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/reflection/ReflectionAdapter2.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.services / src / org / simantics / db / services / adaption / reflection / ReflectionAdapter2.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.services.adaption.reflection;
13
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.Arrays;
17
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;
25
26 public class ReflectionAdapter2<T> implements Adapter<T, Resource> {
27
28         Constructor<? extends T> constructor;
29         IDynamicAdapter2[] parameters;
30         
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;
37         }
38         
39     @Override
40     public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {
41
42         if(parameters.length == 0) {
43             
44 //            System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);
45
46             try {
47                 procedure.execute(g, constructor.newInstance());
48             } catch (IllegalArgumentException e) {
49                 procedure.exception(g, e);
50                 e.printStackTrace();
51             } catch (InstantiationException e) {
52                 procedure.exception(g, e);
53                 e.printStackTrace();
54             } catch (IllegalAccessException e) {
55                 procedure.exception(g, e);
56                 e.printStackTrace();
57             } catch (InvocationTargetException e) {
58                 procedure.exception(g, e.getCause());
59                 e.getCause().printStackTrace();
60             }
61             
62         } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {
63
64             try {
65                 procedure.execute(g, constructor.newInstance(r));
66             } catch (IllegalArgumentException e) {
67                 procedure.exception(g, e);
68                 e.printStackTrace();
69             } catch (InstantiationException e) {
70                 procedure.exception(g, e);
71                 e.printStackTrace();
72             } catch (IllegalAccessException e) {
73                 procedure.exception(g, e);
74                 e.printStackTrace();
75             } catch (InvocationTargetException e) {
76                 procedure.exception(g, e.getCause());
77                 e.getCause().printStackTrace();
78             }
79                 
80         } else {
81         
82             g.asyncRequest(new ReadRequest() {
83     
84                 @Override
85                 public void run(ReadGraph graph) throws DatabaseException {
86                         
87                     Object[] args = new Object[parameters.length];
88                     try {
89                         for(int i=0;i<parameters.length;++i)
90                                 args[i] = parameters[i].adapt(graph, r);
91                         procedure.execute(g, constructor.newInstance(args));
92                                 } catch (IllegalArgumentException e) {
93                                     procedure.exception(g, e);
94                                         e.printStackTrace();
95                                 } catch (InstantiationException e) {
96                         procedure.exception(g, e);
97                                         e.printStackTrace();
98                                 } catch (IllegalAccessException e) {
99                         procedure.exception(g, e);
100                                         e.printStackTrace();
101                                 } catch (InvocationTargetException e) {
102                         procedure.exception(g, e.getCause());
103                                         e.getCause().printStackTrace();
104                                 } catch (DatabaseException e) {
105                         procedure.exception(g, e);
106                                         e.printStackTrace();
107                                 } catch (Throwable t) {
108                                     procedure.exception(g, t);
109                                     t.printStackTrace();
110                                 }
111                 }
112                 
113                 @Override
114                 public String toString() {
115                         return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);
116                 }
117                 
118             });
119         
120         }
121         
122     }
123     
124     @Override
125     public String toString() {
126         return "ReflectionAdapter for " + constructor.getName();
127     }
128
129 }