|
1 | 1 | /* |
| 2 | + * Elemental |
| 3 | + * Copyright (C) 2024, Evolved Binary Ltd |
| 4 | + * |
| 5 | + |
| 6 | + * https://www.evolvedbinary.com | https://www.elemental.xyz |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; version 2.1. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this library; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + * |
| 21 | + * NOTE: Parts of this file contain code from 'The eXist-db Authors'. |
| 22 | + * The original license header is included below. |
| 23 | + * |
| 24 | + * ===================================================================== |
| 25 | + * |
2 | 26 | * eXist-db Open Source Native XML Database |
3 | 27 | * Copyright (C) 2001 The eXist-db Authors |
4 | 28 | * |
@@ -39,119 +63,146 @@ public class CollectionTriggers implements CollectionTrigger { |
39 | 63 |
|
40 | 64 | private final List<CollectionTrigger> triggers; |
41 | 65 |
|
42 | | - public CollectionTriggers(DBBroker broker, Txn transaction) throws TriggerException { |
| 66 | + public CollectionTriggers(final DBBroker broker, final Txn transaction) throws TriggerException { |
43 | 67 | this(broker, transaction, null, null); |
44 | 68 | } |
45 | 69 |
|
46 | | - public CollectionTriggers(DBBroker broker, Txn transaction, Collection collection) throws TriggerException { |
| 70 | + public CollectionTriggers(final DBBroker broker, final Txn transaction, final Collection collection) throws TriggerException { |
47 | 71 | this(broker, transaction, collection, collection.getConfiguration(broker)); |
48 | 72 | } |
49 | 73 |
|
50 | | - public CollectionTriggers(DBBroker broker, Txn transaction, Collection collection, CollectionConfiguration config) throws TriggerException { |
51 | | - |
52 | | - List<TriggerProxy<? extends CollectionTrigger>> colTriggers = null; |
53 | | - if (config != null) { |
54 | | - colTriggers = config.collectionTriggers(); |
55 | | - } |
56 | | - |
57 | | - java.util.Collection<TriggerProxy<? extends CollectionTrigger>> masterTriggers = broker.getDatabase().getCollectionTriggers(); |
| 74 | + public CollectionTriggers(final DBBroker broker, final Txn transaction, final Collection collection, final CollectionConfiguration config) throws TriggerException { |
| 75 | + final List<TriggerProxy<? extends CollectionTrigger>> colTriggers = config != null ? config.collectionTriggers() : null; |
| 76 | + final java.util.Collection<TriggerProxy<? extends CollectionTrigger>> masterTriggers = broker.getDatabase().getCollectionTriggers(); |
58 | 77 |
|
59 | 78 | triggers = new ArrayList<>(masterTriggers.size() + (colTriggers == null ? 0 : colTriggers.size())); |
60 | 79 |
|
61 | | - for (TriggerProxy<? extends CollectionTrigger> colTrigger : masterTriggers) { |
62 | | - |
63 | | - CollectionTrigger instance = colTrigger.newInstance(broker, transaction, collection); |
64 | | - |
| 80 | + for (final TriggerProxy<? extends CollectionTrigger> colTrigger : masterTriggers) { |
| 81 | + final CollectionTrigger instance = colTrigger.newInstance(broker, transaction, collection); |
65 | 82 | register(instance); |
66 | 83 | } |
67 | 84 |
|
68 | 85 | if (colTriggers != null) { |
69 | | - for (TriggerProxy<? extends CollectionTrigger> colTrigger : colTriggers) { |
70 | | - |
71 | | - CollectionTrigger instance = colTrigger.newInstance(broker, transaction, collection); |
72 | | - |
| 86 | + for (final TriggerProxy<? extends CollectionTrigger> colTrigger : colTriggers) { |
| 87 | + final CollectionTrigger instance = colTrigger.newInstance(broker, transaction, collection); |
73 | 88 | register(instance); |
74 | 89 | } |
75 | 90 | } |
76 | 91 | } |
77 | 92 |
|
78 | | - private void register(CollectionTrigger trigger) { |
| 93 | + private void register(final CollectionTrigger trigger) { |
79 | 94 | triggers.add(trigger); |
80 | 95 | } |
81 | 96 |
|
82 | 97 | @Override |
83 | | - public void configure(DBBroker broker, Txn transaction, Collection col, Map<String, List<? extends Object>> parameters) throws TriggerException { |
| 98 | + public void configure(final DBBroker broker, final Txn transaction, final Collection col, final Map<String, List<? extends Object>> parameters) throws TriggerException { |
84 | 99 | } |
85 | 100 |
|
86 | 101 | @Override |
87 | | - public void beforeCreateCollection(DBBroker broker, Txn txn, XmldbURI uri) throws TriggerException { |
88 | | - for (CollectionTrigger trigger : triggers) { |
89 | | - trigger.beforeCreateCollection(broker, txn, uri); |
| 102 | + public void beforeCreateCollection(final DBBroker broker, final Txn txn, final XmldbURI uri) throws TriggerException { |
| 103 | + for (final CollectionTrigger trigger : triggers) { |
| 104 | + try { |
| 105 | + trigger.beforeCreateCollection(broker, txn, uri); |
| 106 | + } catch (final Exception e) { |
| 107 | + logAndThrowError("beforeCreateCollection", trigger, uri, e); |
| 108 | + } |
90 | 109 | } |
91 | 110 | } |
92 | 111 |
|
93 | 112 | @Override |
94 | | - public void afterCreateCollection(DBBroker broker, Txn txn, Collection collection) { |
95 | | - for (CollectionTrigger trigger : triggers) { |
| 113 | + public void afterCreateCollection(final DBBroker broker, final Txn txn, final Collection collection) { |
| 114 | + for (final CollectionTrigger trigger : triggers) { |
96 | 115 | try { |
97 | 116 | trigger.afterCreateCollection(broker, txn, collection); |
98 | | - } catch (Exception e) { |
99 | | - Trigger.LOG.error(e.getMessage(), e); |
| 117 | + } catch (final Exception e) { |
| 118 | + logError("afterCreateCollection", trigger, collection.getURI(), e); |
100 | 119 | } |
101 | 120 | } |
102 | 121 | } |
103 | 122 |
|
104 | 123 | @Override |
105 | | - public void beforeCopyCollection(DBBroker broker, Txn txn, Collection collection, XmldbURI newUri) throws TriggerException { |
106 | | - for (CollectionTrigger trigger : triggers) { |
107 | | - trigger.beforeCopyCollection(broker, txn, collection, newUri); |
| 124 | + public void beforeCopyCollection(final DBBroker broker, final Txn txn, final Collection collection, final XmldbURI newUri) throws TriggerException { |
| 125 | + for (final CollectionTrigger trigger : triggers) { |
| 126 | + try { |
| 127 | + trigger.beforeCopyCollection(broker, txn, collection, newUri); |
| 128 | + } catch (final Exception e) { |
| 129 | + logAndThrowError("beforeCopyCollection", trigger, collection.getURI(), e); |
| 130 | + } |
108 | 131 | } |
109 | 132 | } |
110 | 133 |
|
111 | 134 | @Override |
112 | | - public void afterCopyCollection(DBBroker broker, Txn txn, Collection collection, XmldbURI oldUri) { |
113 | | - for (CollectionTrigger trigger : triggers) { |
| 135 | + public void afterCopyCollection(final DBBroker broker, final Txn txn, final Collection collection, final XmldbURI oldUri) { |
| 136 | + for (final CollectionTrigger trigger : triggers) { |
114 | 137 | try { |
115 | 138 | trigger.afterCopyCollection(broker, txn, collection, oldUri); |
116 | | - } catch (Exception e) { |
117 | | - Trigger.LOG.error(e.getMessage(), e); |
| 139 | + } catch (final Exception e) { |
| 140 | + logError("afterCopyCollection", trigger, oldUri, e); |
118 | 141 | } |
119 | 142 | } |
120 | 143 | } |
121 | 144 |
|
122 | 145 | @Override |
123 | | - public void beforeMoveCollection(DBBroker broker, Txn txn, Collection collection, XmldbURI newUri) throws TriggerException { |
124 | | - for (CollectionTrigger trigger : triggers) { |
125 | | - trigger.beforeMoveCollection(broker, txn, collection, newUri); |
| 146 | + public void beforeMoveCollection(final DBBroker broker, final Txn txn, final Collection collection, final XmldbURI newUri) throws TriggerException { |
| 147 | + for (final CollectionTrigger trigger : triggers) { |
| 148 | + try { |
| 149 | + trigger.beforeMoveCollection(broker, txn, collection, newUri); |
| 150 | + } catch (final Exception e) { |
| 151 | + logAndThrowError("beforeMoveCollection", trigger, collection.getURI(), e); |
| 152 | + } |
126 | 153 | } |
127 | 154 | } |
128 | 155 |
|
129 | 156 | @Override |
130 | | - public void afterMoveCollection(DBBroker broker, Txn txn, Collection collection, XmldbURI oldUri) { |
131 | | - for (CollectionTrigger trigger : triggers) { |
| 157 | + public void afterMoveCollection(final DBBroker broker, final Txn txn, final Collection collection, final XmldbURI oldUri) { |
| 158 | + for (final CollectionTrigger trigger : triggers) { |
132 | 159 | try { |
133 | 160 | trigger.afterMoveCollection(broker, txn, collection, oldUri); |
134 | | - } catch (Exception e) { |
135 | | - Trigger.LOG.error(e.getMessage(), e); |
| 161 | + } catch (final Exception e) { |
| 162 | + logError("afterMoveCollection", trigger, oldUri, e); |
136 | 163 | } |
137 | 164 | } |
138 | 165 | } |
139 | 166 |
|
140 | 167 | @Override |
141 | | - public void beforeDeleteCollection(DBBroker broker, Txn txn, Collection collection) throws TriggerException { |
142 | | - for (CollectionTrigger trigger : triggers) { |
143 | | - trigger.beforeDeleteCollection(broker, txn, collection); |
| 168 | + public void beforeDeleteCollection(final DBBroker broker, final Txn txn, final Collection collection) throws TriggerException { |
| 169 | + for (final CollectionTrigger trigger : triggers) { |
| 170 | + try { |
| 171 | + trigger.beforeDeleteCollection(broker, txn, collection); |
| 172 | + } catch (final Exception e) { |
| 173 | + logAndThrowError("beforeDeleteCollection", trigger, collection.getURI(), e); |
| 174 | + } |
144 | 175 | } |
145 | 176 | } |
146 | 177 |
|
147 | 178 | @Override |
148 | | - public void afterDeleteCollection(DBBroker broker, Txn txn, XmldbURI uri) { |
149 | | - for (CollectionTrigger trigger : triggers) { |
| 179 | + public void afterDeleteCollection(final DBBroker broker, final Txn txn, final XmldbURI uri) { |
| 180 | + for (final CollectionTrigger trigger : triggers) { |
150 | 181 | try { |
151 | 182 | trigger.afterDeleteCollection(broker, txn, uri); |
152 | | - } catch (Exception e) { |
153 | | - Trigger.LOG.error(e.getMessage(), e); |
| 183 | + } catch (final Exception e) { |
| 184 | + logError("afterDeleteCollection", trigger, uri, e); |
154 | 185 | } |
155 | 186 | } |
156 | 187 | } |
| 188 | + |
| 189 | + private void logAndThrowError(final String eventName, final CollectionTrigger collectionTrigger, final XmldbURI source, final Exception e) throws TriggerException { |
| 190 | + logError(eventName, collectionTrigger, source, e); |
| 191 | + throwError(e); |
| 192 | + } |
| 193 | + |
| 194 | + private void logError(final String eventName, final CollectionTrigger collectionTrigger, final XmldbURI source, final Exception e) { |
| 195 | + final String message = String.format("Error in %s#%s triggered by: %s, %s", collectionTrigger.getClass().getSimpleName(), eventName, source, e.getMessage()); |
| 196 | + Trigger.LOG.error(message, e); |
| 197 | + } |
| 198 | + |
| 199 | + private void throwError(final Exception e) throws TriggerException { |
| 200 | + if (e instanceof TriggerException) { |
| 201 | + throw (TriggerException) e; |
| 202 | + } else if (e instanceof RuntimeException) { |
| 203 | + throw (RuntimeException) e; |
| 204 | + } else { |
| 205 | + throw new TriggerException(e); |
| 206 | + } |
| 207 | + } |
157 | 208 | } |
0 commit comments