]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/reflection/ReflectionAdapter2.java
Fixed multiple issues causing dangling references to discarded queries
[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.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;
26
27 public class ReflectionAdapter2<T> implements Adapter<T, Resource> {
28
29         private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionAdapter2.class);
30
31         Constructor<? extends T> constructor;
32         IDynamicAdapter2[] parameters;
33         
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;
40         }
41         
42     @Override
43     public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {
44
45         if(parameters.length == 0) {
46             
47             try {
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());
61             }
62             
63         } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {
64
65             try {
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());
79             }
80                 
81         } else {
82         
83             g.asyncRequest(new AsyncRead<T>() {
84                 
85                 @Override
86                 public void perform(AsyncReadGraph graph, AsyncProcedure<T> p) {
87                     
88                     Object[] args = new Object[parameters.length];
89                     try {
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) {
94                         p.exception(g, e);
95                         LOGGER.error("Unexpected exception during adapter creation", e);
96                     } catch (InstantiationException e) {
97                         p.exception(g, e);
98                         LOGGER.error("Unexpected exception during adapter creation", e);
99                     } catch (IllegalAccessException e) {
100                         p.exception(g, 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) {
106                         p.exception(g, e);
107                         LOGGER.error("Unexpected exception during adapter creation", e);
108                     } catch (Throwable t) {
109                         p.exception(g, t);
110                         LOGGER.error("Unexpected exception during adapter creation", t);
111                     }
112                     
113                 }
114                 
115                 @Override
116                 public String toString() {
117                         return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);
118                 }
119                 
120             }, procedure);
121         
122         }
123         
124     }
125     
126     @Override
127     public String toString() {
128         return "ReflectionAdapter for " + constructor.getName();
129     }
130
131 }