| 1 | package org.jtoolkit.essence.data.impl; |
| 2 | |
| 3 | import org.apache.commons.logging.Log; |
| 4 | import org.apache.commons.logging.LogFactory; |
| 5 | import org.jetbrains.annotations.NotNull; |
| 6 | import org.jetbrains.annotations.Nullable; |
| 7 | import org.jtoolkit.essence.app.net.DataSocketFactory; |
| 8 | import org.jtoolkit.essence.app.net.NetObject; |
| 9 | import org.jtoolkit.essence.app.pojo.impl.DataValueClass; |
| 10 | import org.jtoolkit.essence.data.Store; |
| 11 | import static org.jtoolkit.essence.data.Store.*; |
| 12 | import static org.jtoolkit.essence.data.impl.NetStore.newNetStore; |
| 13 | import static org.jtoolkit.essence.utils.StringUtils.isBlank; |
| 14 | import org.jtoolkit.essence.utils.Named; |
| 15 | |
| 16 | import java.util.LinkedHashMap; |
| 17 | import java.util.Map; |
| 18 | import java.util.Set; |
| 19 | |
| 20 | /* |
| 21 | Copyright 2006 Peter Lawrey |
| 22 | |
| 23 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 24 | you may not use this file except in compliance with the License. |
| 25 | You may obtain a copy of the License at |
| 26 | |
| 27 | http://www.apache.org/licenses/LICENSE-2.0 |
| 28 | |
| 29 | Unless required by applicable law or agreed to in writing, software |
| 30 | distributed under the License is distributed on an "AS IS" BASIS, |
| 31 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 32 | See the License for the specific language governing permissions and |
| 33 | limitations under the License. |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * @author Peter Lawrey |
| 38 | */ |
| 39 | public class StoreFactory { |
| 40 | private static final Log LOG = LogFactory.getLog(StoreFactory.class); |
| 41 | private final Map<String, Store> stores = new LinkedHashMap<String, Store>(); |
| 42 | |
| 43 | public <K, V> Store<K, V> acquire(@NotNull String containerName, @NotNull String name, @Nullable CollectionType collectionType, |
| 44 | @NotNull StoreType storeType, @NotNull Class<V> valueClass, @Nullable String location, |
| 45 | @Nullable ReadMode readMode, @Nullable PersistMode persistMode, @NotNull Named.Source<String> options) throws Exception { |
| 46 | DataValueClass<V> valueType = DataValueClass.acquire(valueClass); |
| 47 | DataValueClass<K> key = valueType.getInferedKey(true); |
| 48 | return acquire(containerName, name, collectionType, storeType, key, valueType, location, readMode, persistMode, options); |
| 49 | } |
| 50 | |
| 51 | public synchronized <K, V> Store<K, V> acquire(@NotNull String containerName, @NotNull String name, @Nullable CollectionType collectionType, |
| 52 | @Nullable StoreType storeType, @Nullable DataValueClass<K> keyClass, @NotNull DataValueClass<V> valueClass, |
| 53 | @Nullable String location, @Nullable ReadMode readMode, @Nullable PersistMode persistMode, |
| 54 | @NotNull Named.Source<String> options) throws Exception { |
| 55 | //noinspection unchecked |
| 56 | Store<K, V> store = stores.get(name); |
| 57 | if (store == null) |
| 58 | stores.put(name, store = acquire0(containerName, name, collectionType, storeType, keyClass, valueClass, location, readMode, persistMode, options)); |
| 59 | return store; |
| 60 | } |
| 61 | |
| 62 | @SuppressWarnings({"RedundantThrows", "OverlyComplexMethod", "OverlyLongMethod"}) |
| 63 | private static <K, V> Store<K, V> acquire0(String containerName, @NotNull String name, |
| 64 | @Nullable CollectionType collectionType, @Nullable StoreType storeType, |
| 65 | @Nullable DataValueClass<K> keyClass, @NotNull DataValueClass<V> valueClass, |
| 66 | @Nullable String location, @Nullable ReadMode readMode, |
| 67 | @Nullable PersistMode persistMode, @NotNull Named.Source<String> options) |
| 68 | throws Exception { |
| 69 | if (collectionType == null) collectionType = CollectionType.ANY; |
| 70 | if (storeType == null) storeType = StoreType.FILE_SET; |
| 71 | if (readMode == null) readMode = ReadMode.ANY; |
| 72 | if (persistMode == null) persistMode = PersistMode.ANY; |
| 73 | if (location == null) location = ""; |
| 74 | |
| 75 | Set<String> optionNames = options.getNames(); |
| 76 | switch (storeType) { |
| 77 | case MEMORY: |
| 78 | if (readMode != ReadMode.ANY && readMode != ReadMode.NONE) |
| 79 | LOG.warn(name + ": ReadMode " + readMode + " ignored for Memory store, only NONE supported"); |
| 80 | if (persistMode != PersistMode.ANY && persistMode != PersistMode.NONE) |
| 81 | LOG.warn(name + ": PersistMode " + persistMode + " ignored for Memory store, only NONE supported"); |
| 82 | if (!optionNames.isEmpty()) |
| 83 | LOG.warn(name + ": Options ignored for Memory store, was=" + options); |
| 84 | return new MemoryStore<K, V>(name, collectionType, keyClass, valueClass); |
| 85 | |
| 86 | case TABS: |
| 87 | if (isBlank(location)) |
| 88 | location = name + ".tabs"; |
| 89 | if (readMode != ReadMode.ANY && readMode != ReadMode.ALL) |
| 90 | LOG.warn(name + ": ReadMode " + readMode + " ignored for Tabs store, only ALL supported"); |
| 91 | if (persistMode != PersistMode.ANY && persistMode != PersistMode.ASYNC && persistMode != PersistMode.NONE && persistMode != PersistMode.READ_ONLY) |
| 92 | LOG.warn(name + ": PersistMode " + persistMode + " ignored for Tabs store, only ASYNC, NONE & READ_ONLY supported"); |
| 93 | if (!optionNames.isEmpty()) |
| 94 | LOG.warn(name + ": Options ignored for Tabs store, was=" + options); |
| 95 | return new TabStore<K, V>(name, collectionType, keyClass, valueClass, location); |
| 96 | |
| 97 | case SINGLE_FILE: |
| 98 | if (isBlank(location)) |
| 99 | location = name + ".data"; |
| 100 | if (readMode != ReadMode.ANY && readMode != ReadMode.ALL) |
| 101 | LOG.warn(name + ": ReadMode " + readMode + " ignored for Single file store, only ALL supported"); |
| 102 | if (persistMode != PersistMode.ANY && persistMode != PersistMode.ASYNC && persistMode != PersistMode.SEMI_SYNC && persistMode != PersistMode.NONE && persistMode != PersistMode.READ_ONLY) |
| 103 | LOG.warn(name + ": PersistMode " + persistMode + " ignored for Single file store, only ASYNC, SEMI_SYNC, NONE & READ_ONLY supported"); |
| 104 | if (!optionNames.isEmpty()) |
| 105 | LOG.warn(name + ": Options ignored for File set store, was=" + options); |
| 106 | return new DatableStore<K, V>(name, collectionType, keyClass, valueClass, location); |
| 107 | |
| 108 | case FILE_SET: |
| 109 | if (isBlank(location)) |
| 110 | location = name + "-set"; |
| 111 | if (readMode != ReadMode.ANY && readMode != ReadMode.ALL && readMode != ReadMode.NONE) |
| 112 | LOG.warn(name + ": ReadMode " + readMode + " ignored for File set store, only ALL, NONE supported"); |
| 113 | if (!optionNames.isEmpty()) |
| 114 | LOG.warn(name + ": Options ignored for File set store, was=" + options); |
| 115 | return new FileSetStore<K, V>(name, collectionType, keyClass, valueClass, location, readMode, persistMode); |
| 116 | |
| 117 | case JDBC: |
| 118 | throw new UnsupportedOperationException("JDBC not yet supported"); |
| 119 | /* |
| 120 | if (location.length() == 0) |
| 121 | throw new IllegalArgumentException(name + ": The location must be set to the jdbc url."); |
| 122 | if (readMode != ReadMode.ANY && readMode != ReadMode.ALL) |
| 123 | LOG.warn(name + ": ReadMode " + readMode + " ignored for Jdbc store, only ALL supported"); |
| 124 | if (persistMode != PersistMode.ANY && persistMode != PersistMode.SYNC) |
| 125 | LOG.warn(name + ": PersistMode " + persistMode + " ignored for Jdbc store, only SYNC supported"); |
| 126 | String tableName = options.remove("table"); |
| 127 | if (tableName == null) |
| 128 | tableName = name; |
| 129 | ConnectionPoolDataSource connectionPool = acquireConnectionPool(location, options); |
| 130 | if (!options.isEmpty()) |
| 131 | LOG.warn(name + ": Options ignored for Jdbc store, was=" + options); |
| 132 | return new JdbcStore<K, V>(name, collectionType, keyClass, valueClass, connectionPool, tableName); |
| 133 | */ |
| 134 | case NET: |
| 135 | if (isBlank(location)) |
| 136 | throw new IllegalArgumentException(name + ": The location must be set to the net url."); |
| 137 | if (readMode != ReadMode.ANY && readMode != ReadMode.MINIMAL) |
| 138 | LOG.warn(name + ": ReadMode " + readMode + " ignored for Net store, only MINIMAL supported"); |
| 139 | if (persistMode != PersistMode.ANY && persistMode != PersistMode.SYNC && persistMode != PersistMode.READ_ONLY) |
| 140 | LOG.warn(name + ": PersistMode " + persistMode + " ignored for Net store, only SYNC,READ_ONLY supported"); |
| 141 | String componentName = options.getValue("component"); |
| 142 | if (componentName == null) |
| 143 | componentName = name; |
| 144 | optionNames.remove("components"); |
| 145 | if (!optionNames.isEmpty()) |
| 146 | LOG.warn(name + ": Options ignored for Net store, was=" + options); |
| 147 | NetObject<Store<K, V>> netObject = new NetObject<Store<K, V>>(location, componentName, acquireDataSocketFactory(containerName, location)); |
| 148 | return newNetStore(collectionType, netObject); |
| 149 | } |
| 150 | throw new InternalError("Should get to here."); |
| 151 | } |
| 152 | |
| 153 | private static DataSocketFactory acquireDataSocketFactory(String containerName, String location) { |
| 154 | return new DataSocketFactory(containerName, location); |
| 155 | } |
| 156 | } |