From 6dd20a4b23592c0a9a6be23a2123fef410981a2e Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 7 Oct 2006 22:27:25 +0000 Subject: Added TriggerFactory. Deprecated wrapped storage and query. --- .../com/amazon/carbonado/RepositoryBuilder.java | 8 +++++ .../java/com/amazon/carbonado/TriggerFactory.java | 38 ++++++++++++++++++++++ .../amazon/carbonado/repo/jdbc/JDBCRepository.java | 4 +++ .../carbonado/repo/jdbc/JDBCRepositoryBuilder.java | 3 +- .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 3 +- .../carbonado/spi/AbstractRepositoryBuilder.java | 22 +++++++++++++ .../amazon/carbonado/spi/CommonMethodNames.java | 1 + .../amazon/carbonado/spi/StorableGenerator.java | 1 + .../com/amazon/carbonado/spi/TriggerManager.java | 15 ++++++++- .../com/amazon/carbonado/spi/WrappedQuery.java | 1 + .../com/amazon/carbonado/spi/WrappedStorage.java | 4 ++- .../com/amazon/carbonado/spi/WrappedSupport.java | 1 + 12 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/amazon/carbonado/TriggerFactory.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/amazon/carbonado/RepositoryBuilder.java b/src/main/java/com/amazon/carbonado/RepositoryBuilder.java index 179d902..368ed9a 100644 --- a/src/main/java/com/amazon/carbonado/RepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/RepositoryBuilder.java @@ -99,4 +99,12 @@ public interface RepositoryBuilder { * @see com.amazon.carbonado.repo.replicated.ReplicatedRepositoryBuilder */ void setMaster(boolean b); + + /** + * Optionally add a TriggerFactory which will be called upon to create an + * initial trigger for each Storable type that the Repository supports. The + * primary purpose of this method is to allow decorator repositories the + * opportunity to register custom persistence code for each Storable. + */ + void addTriggerFactory(TriggerFactory factory); } diff --git a/src/main/java/com/amazon/carbonado/TriggerFactory.java b/src/main/java/com/amazon/carbonado/TriggerFactory.java new file mode 100644 index 0000000..5193267 --- /dev/null +++ b/src/main/java/com/amazon/carbonado/TriggerFactory.java @@ -0,0 +1,38 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.amazon.carbonado; + +/** + * Can be used with {@link RepositoryBuilder} to automatically register + * triggers as Storable types become available. + * + * @author Brian S O'Neill + */ +public interface TriggerFactory { + /** + * Return an appropriate trigger for the given type, or null if none. This + * method is expected to be called at most once per Storable type. As an + * extra safeguard, trigger implementations are encouraged to implement the + * equals method. + * + * @param type Storable type requesting an automatic trigger + * @return trigger instance or null if not applicable + */ + Trigger triggerFor(Class type); +} diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java index bcdd0c4..6e7bf2f 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java @@ -43,6 +43,7 @@ import com.amazon.carbonado.PersistException; import com.amazon.carbonado.Repository; import com.amazon.carbonado.RepositoryException; import com.amazon.carbonado.Transaction; +import com.amazon.carbonado.TriggerFactory; import com.amazon.carbonado.UnsupportedTypeException; import com.amazon.carbonado.capability.Capability; @@ -135,6 +136,7 @@ public class JDBCRepository private final String mName; final boolean mIsMaster; + final Iterable mTriggerFactories; private final AtomicReference mRootRef; private final String mDatabaseProductName; private final DataSource mDataSource; @@ -179,6 +181,7 @@ public class JDBCRepository @SuppressWarnings("unchecked") JDBCRepository(AtomicReference rootRef, String name, boolean isMaster, + Iterable triggerFactories, DataSource dataSource, String catalog, String schema) throws RepositoryException { @@ -187,6 +190,7 @@ public class JDBCRepository } mName = name; mIsMaster = isMaster; + mTriggerFactories = triggerFactories; mRootRef = rootRef; mDataSource = dataSource; mCatalog = catalog; diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java index 3a797e8..5b59852 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java @@ -72,7 +72,8 @@ public class JDBCRepositoryBuilder extends AbstractRepositoryBuilder { public JDBCRepository build(AtomicReference rootRef) throws RepositoryException { assertReady(); JDBCRepository repo = new JDBCRepository - (rootRef, getName(), isMaster(), getDataSource(), mCatalog, mSchema); + (rootRef, getName(), isMaster(), getTriggerFactories(), + getDataSource(), mCatalog, mSchema); rootRef.set(repo); return repo; } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index 5d4a4a2..08cdb46 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -110,7 +110,8 @@ class JDBCStorage extends StandardQueryFactory mExecutorFactory = new QueryExecutorCache(new ExecutorFactory()); - mTriggerManager = new TriggerManager(); + mTriggerManager = new TriggerManager + (info.getStorableType(), repository.mTriggerFactories); } public Class getStorableType() { diff --git a/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java index a8f0169..d74c70a 100644 --- a/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/spi/AbstractRepositoryBuilder.java @@ -20,6 +20,8 @@ package com.amazon.carbonado.spi; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.concurrent.atomic.AtomicReference; @@ -27,6 +29,8 @@ import com.amazon.carbonado.ConfigurationException; import com.amazon.carbonado.Repository; import com.amazon.carbonado.RepositoryBuilder; import com.amazon.carbonado.RepositoryException; +import com.amazon.carbonado.Trigger; +import com.amazon.carbonado.TriggerFactory; /** * Abstract builder class for opening repositories. @@ -35,13 +39,20 @@ import com.amazon.carbonado.RepositoryException; * @author Brian S O'Neill */ public abstract class AbstractRepositoryBuilder implements RepositoryBuilder { + private final List mTriggerFactories; + protected AbstractRepositoryBuilder() { + mTriggerFactories = new ArrayList(2); } public Repository build() throws ConfigurationException, RepositoryException { return build(new AtomicReference()); } + public void addTriggerFactory(TriggerFactory factory) { + mTriggerFactories.add(factory); + } + /** * Throw a configuration exception if the configuration is not filled out * sufficiently and correctly such that a repository could be instantiated @@ -80,4 +91,15 @@ public abstract class AbstractRepositoryBuilder implements RepositoryBuilder { messages.add("name missing"); } } + + /** + * Returns all the TriggerFactories which were added. + */ + protected Iterable getTriggerFactories() { + if (mTriggerFactories == null || mTriggerFactories.size() == 0) { + return Collections.emptyList(); + } else { + return new ArrayList(mTriggerFactories); + } + } } diff --git a/src/main/java/com/amazon/carbonado/spi/CommonMethodNames.java b/src/main/java/com/amazon/carbonado/spi/CommonMethodNames.java index 8a011c9..c79e502 100644 --- a/src/main/java/com/amazon/carbonado/spi/CommonMethodNames.java +++ b/src/main/java/com/amazon/carbonado/spi/CommonMethodNames.java @@ -83,5 +83,6 @@ public class CommonMethodNames { EXIT_METHOD_NAME = "exit"; /** WrappedStorage.Support API method name */ + @Deprecated public static final String CREATE_WRAPPED_SUPPORT_METHOD_NAME = "createSupport"; } diff --git a/src/main/java/com/amazon/carbonado/spi/StorableGenerator.java b/src/main/java/com/amazon/carbonado/spi/StorableGenerator.java index ecd344d..f69d4dd 100644 --- a/src/main/java/com/amazon/carbonado/spi/StorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/spi/StorableGenerator.java @@ -357,6 +357,7 @@ public final class StorableGenerator { * @throws IllegalArgumentException if type is null */ @SuppressWarnings("unchecked") + @Deprecated public static Class getWrappedClass(Class type) throws IllegalArgumentException { diff --git a/src/main/java/com/amazon/carbonado/spi/TriggerManager.java b/src/main/java/com/amazon/carbonado/spi/TriggerManager.java index cabf526..60661d1 100644 --- a/src/main/java/com/amazon/carbonado/spi/TriggerManager.java +++ b/src/main/java/com/amazon/carbonado/spi/TriggerManager.java @@ -26,6 +26,7 @@ import java.util.Arrays; import com.amazon.carbonado.PersistException; import com.amazon.carbonado.Storable; import com.amazon.carbonado.Trigger; +import com.amazon.carbonado.TriggerFactory; /** * Used by Storage implementations to manage triggers and consolidate them into @@ -93,7 +94,19 @@ public class TriggerManager { private volatile ForUpdate mForUpdate; private volatile ForDelete mForDelete; - public TriggerManager() { + /** + * @param triggerFactories TriggerFactories which will be called upon to + * optionally return a trigger to initially register + */ + public TriggerManager(Class type, Iterable triggerFactories) { + if (triggerFactories != null) { + for (TriggerFactory factory : triggerFactories) { + Trigger trigger = factory.triggerFor(type); + if (trigger != null) { + addTrigger(trigger); + } + } + } } /** diff --git a/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java b/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java index ec3ae34..531f38f 100644 --- a/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java +++ b/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java @@ -36,6 +36,7 @@ import com.amazon.carbonado.filter.FilterValues; * @author Don Schneider * @author Brian S O'Neill */ +@Deprecated public abstract class WrappedQuery implements Query { // The query to which this query will delegate diff --git a/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java b/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java index 598e889..629cb53 100644 --- a/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java +++ b/src/main/java/com/amazon/carbonado/spi/WrappedStorage.java @@ -39,6 +39,7 @@ import com.amazon.carbonado.util.QuickConstructorGenerator; * * @author Brian S O'Neill */ +@Deprecated public abstract class WrappedStorage implements Storage { private final Storage mStorage; private final WrappedStorableFactory mFactory; @@ -53,7 +54,8 @@ public abstract class WrappedStorage implements Storage { .getWrappedClass(storage.getStorableType()); mFactory = QuickConstructorGenerator .getInstance(wrappedClass, WrappedStorableFactory.class); - mTriggerManager = new TriggerManager(); + // FIXME: wrong, just deprecate this class + mTriggerManager = new TriggerManager(null, null); } public Class getStorableType() { diff --git a/src/main/java/com/amazon/carbonado/spi/WrappedSupport.java b/src/main/java/com/amazon/carbonado/spi/WrappedSupport.java index 24e2c02..fa57c60 100644 --- a/src/main/java/com/amazon/carbonado/spi/WrappedSupport.java +++ b/src/main/java/com/amazon/carbonado/spi/WrappedSupport.java @@ -27,6 +27,7 @@ import com.amazon.carbonado.Storable; * * @author Brian S O'Neill */ +@Deprecated public interface WrappedSupport extends TriggerSupport { /** * @see Storable#load -- cgit v1.2.3