/
AbstractTriggerProxy.java
116 lines (95 loc) · 4.53 KB
/
AbstractTriggerProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* eXist Open Source Native XML Database
* Copyright (C) 2011-2012 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* $Id$
*/
package org.exist.collections.triggers;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.exist.collections.Collection;
import org.exist.security.PermissionDeniedException;
import org.exist.storage.DBBroker;
import org.exist.xmldb.XmldbURI;
/**
*
* @author aretter
*/
public abstract class AbstractTriggerProxy<T extends Trigger> implements TriggerProxy<T> {
private final Class<T> clazz;
private Map<String, List<? extends Object>> parameters;
/**
* The database Collection URI of where the configuration for this Trigger came from
* typically somewhere under /db/system/config/db/
*/
private final XmldbURI collectionConfigurationURI;
public AbstractTriggerProxy(Class<? extends T> clazz, XmldbURI collectionConfigurationURI) {
this.clazz = (Class<T>)clazz;
this.collectionConfigurationURI = collectionConfigurationURI;
}
public AbstractTriggerProxy(Class<? extends T> clazz, XmldbURI collectionConfigurationURI, Map<String, List<? extends Object>> parameters) {
this.clazz = (Class<T>)clazz;
this.collectionConfigurationURI = collectionConfigurationURI;
this.parameters = parameters;
}
protected Class<T> getClazz() {
return clazz;
}
protected XmldbURI getCollectionConfigurationURI() {
return collectionConfigurationURI;
}
@Override
public void setParameters(Map<String, List<? extends Object>> parameters) {
this.parameters = parameters;
}
protected Map<String, List<? extends Object>> getParameters() {
return parameters;
}
public T newInstance(DBBroker broker) throws TriggerException {
try {
final T trigger = getClazz().newInstance();
XmldbURI collectionForTrigger = getCollectionConfigurationURI();
if(collectionForTrigger.startsWith(XmldbURI.CONFIG_COLLECTION_URI)) {
collectionForTrigger = collectionForTrigger.trimFromBeginning(XmldbURI.CONFIG_COLLECTION_URI);
}
final Collection collection = broker.getCollection(collectionForTrigger);
trigger.configure(broker, collection, getParameters());
return trigger;
} catch (final InstantiationException ie) {
throw new TriggerException("Unable to instantiate Trigger '" + getClazz().getName() + "': " + ie.getMessage(), ie);
} catch (final IllegalAccessException iae) {
throw new TriggerException("Unable to instantiate Trigger '" + getClazz().getName() + "': " + iae.getMessage(), iae);
} catch (final PermissionDeniedException pde) {
throw new TriggerException("Unable to instantiate Trigger '" + getClazz().getName() + "': " + pde.getMessage(), pde);
}
}
public static List<TriggerProxy<? extends Trigger>> newInstance(Class<? extends Trigger> c, XmldbURI collectionConfigurationURI, Map<String, List<? extends Object>> parameters) throws TriggerException {
final List<TriggerProxy<? extends Trigger>> proxies = new ArrayList<TriggerProxy<? extends Trigger>>();
if(DocumentTrigger.class.isAssignableFrom(c)) {
proxies.add(new DocumentTriggerProxy((Class<? extends DocumentTrigger>)c, collectionConfigurationURI, parameters));
}
if(CollectionTrigger.class.isAssignableFrom(c)) {
proxies.add(new CollectionTriggerProxy((Class<? extends CollectionTrigger>)c, collectionConfigurationURI, parameters));
}
if(proxies.isEmpty()) {
throw new TriggerException("Unknown Trigger class type: " + c.getName());
}
return proxies;
}
}