private void buildCacheStore( OperationContext context, LoaderConfigurationBuilder builder, String containerName, ModelNode store, String storeKey, List<Dependency<?>> dependencies) throws OperationFailedException { final Properties properties = new TypedProperties(); if (store.hasDefined(ModelKeys.PROPERTY)) { for (Property property : store.get(ModelKeys.PROPERTY).asPropertyList()) { // the format of the property elements // "property" => { // "relative-to" => {"value" => "fred"}, // } String propertyName = property.getName(); Property complexValue = property.getValue().asProperty(); String propertyValue = complexValue.getValue().asString(); properties.setProperty(propertyName, propertyValue); } } builder.withProperties(properties); ModelNode resolvedValue = null; if (storeKey.equals(ModelKeys.FILE_STORE)) { builder.cacheLoader(new FileCacheStore()); final String path = ((resolvedValue = CommonAttributes.PATH.resolveModelAttribute(context, store)) .isDefined()) ? resolvedValue.asString() : InfinispanExtension.SUBSYSTEM_NAME + File.separatorChar + containerName; final String relativeTo = ((resolvedValue = CommonAttributes.RELATIVE_TO.resolveModelAttribute(context, store)) .isDefined()) ? resolvedValue.asString() : ServerEnvironment.SERVER_DATA_DIR; Injector<PathManager> injector = new SimpleInjector<PathManager>() { volatile PathManager.Callback.Handle callbackHandle; @Override public void inject(PathManager value) { callbackHandle = value.registerCallback( relativeTo, PathManager.ReloadServerCallback.create(), PathManager.Event.UPDATED, PathManager.Event.REMOVED); properties.setProperty("location", value.resolveRelativePathEntry(path, relativeTo)); } @Override public void uninject() { super.uninject(); if (callbackHandle != null) { callbackHandle.remove(); } } }; dependencies.add( new Dependency<PathManager>( PathManagerService.SERVICE_NAME, PathManager.class, injector)); properties.setProperty("fsyncMode", "perWrite"); } else if (storeKey.equals(ModelKeys.STRING_KEYED_JDBC_STORE) || storeKey.equals(ModelKeys.BINARY_KEYED_JDBC_STORE) || storeKey.equals(ModelKeys.MIXED_KEYED_JDBC_STORE)) { builder.cacheLoader(this.createJDBCStore(properties, context, store)); final String datasource = CommonAttributes.DATA_SOURCE.resolveModelAttribute(context, store).asString(); dependencies.add(new Dependency<Object>(ServiceName.JBOSS.append("data-source", datasource))); properties.setProperty("datasourceJndiLocation", datasource); properties.setProperty("connectionFactoryClass", ManagedConnectionFactory.class.getName()); } else if (storeKey.equals(ModelKeys.REMOTE_STORE)) { builder.cacheLoader(new RemoteCacheStore()); for (ModelNode server : store.require(ModelKeys.REMOTE_SERVERS).asList()) { String outboundSocketBinding = server.get(ModelKeys.OUTBOUND_SOCKET_BINDING).asString(); Injector<OutboundSocketBinding> injector = new SimpleInjector<OutboundSocketBinding>() { @Override public void inject(OutboundSocketBinding value) { try { String address = value.getDestinationAddress().getHostAddress() + ":" + value.getDestinationPort(); String serverList = properties.getProperty("serverList"); properties.setProperty( "serverList", (serverList == null) ? address : serverList + ";" + address); } catch (UnknownHostException e) { throw InfinispanMessages.MESSAGES.failedToInjectSocketBinding(e, value); } } }; dependencies.add( new Dependency<OutboundSocketBinding>( OutboundSocketBinding.OUTBOUND_SOCKET_BINDING_BASE_SERVICE_NAME.append( outboundSocketBinding), OutboundSocketBinding.class, injector)); } if (store.hasDefined(ModelKeys.CACHE)) { properties.setProperty("remoteCacheName", store.get(ModelKeys.CACHE).asString()); properties.setProperty("useDefaultRemoteCache", Boolean.toString(false)); } else { properties.setProperty("useDefaultRemoteCache", Boolean.toString(true)); } if (store.hasDefined(ModelKeys.SOCKET_TIMEOUT)) { properties.setProperty("soTimeout", store.require(ModelKeys.SOCKET_TIMEOUT).asString()); } if (store.hasDefined(ModelKeys.TCP_NO_DELAY)) { properties.setProperty("tcpNoDelay", store.require(ModelKeys.TCP_NO_DELAY).asString()); } } else { String className = store.require(ModelKeys.CLASS).asString(); try { CacheLoader loader = CacheLoader.class .getClassLoader() .loadClass(className) .asSubclass(CacheLoader.class) .newInstance(); builder.cacheLoader(loader); } catch (Exception e) { throw new IllegalArgumentException( String.format("%s is not a valid cache store", className), e); } } }
private void buildCacheStore( LoaderConfigurationBuilder builder, String name, ModelNode store, String storeKey, List<Dependency<?>> dependencies) { final Properties properties = new TypedProperties(); if (store.hasDefined(ModelKeys.PROPERTIES)) { for (Property property : store.get(ModelKeys.PROPERTIES).asPropertyList()) { String propertyName = property.getName(); String propertyValue = property.getValue().asString(); properties.setProperty(propertyName, propertyValue); } } builder.withProperties(properties); if (storeKey.equals(ModelKeys.FILE_STORE)) { builder.cacheLoader(new FileCacheStore()); final String path = store.hasDefined(ModelKeys.PATH) ? store.get(ModelKeys.PATH).asString() : name; Injector<String> injector = new SimpleInjector<String>() { @Override public void inject(String value) { StringBuilder location = new StringBuilder(value); if (path != null) { location.append(File.separatorChar).append(path); } properties.setProperty("location", location.toString()); } }; String relativeTo = store.hasDefined(ModelKeys.RELATIVE_TO) ? store.get(ModelKeys.RELATIVE_TO).asString() : ServerEnvironment.SERVER_DATA_DIR; dependencies.add( new Dependency<String>( AbstractPathService.pathNameOf(relativeTo), String.class, injector)); properties.setProperty("fsyncMode", "perWrite"); } else if (storeKey.equals(ModelKeys.JDBC_STORE)) { builder.cacheLoader(this.createJDBCStore(properties, store)); String datasource = store.require(ModelKeys.DATASOURCE).asString(); dependencies.add( new Dependency<Object>( ServiceName.JBOSS .append("data-source") .append("reference-factory") .append(datasource))); properties.setProperty("datasourceJndiLocation", datasource); properties.setProperty("connectionFactoryClass", ManagedConnectionFactory.class.getName()); } else if (storeKey.equals(ModelKeys.REMOTE_STORE)) { builder.cacheLoader(new RemoteCacheStore()); for (ModelNode server : store.require(ModelKeys.REMOTE_SERVERS).asList()) { String outboundSocketBinding = server.get(ModelKeys.OUTBOUND_SOCKET_BINDING).asString(); Injector<OutboundSocketBinding> injector = new SimpleInjector<OutboundSocketBinding>() { @Override public void inject(OutboundSocketBinding value) { try { String address = value.getDestinationAddress().getHostAddress() + ":" + value.getDestinationPort(); String serverList = properties.getProperty(ConfigurationProperties.SERVER_LIST); properties.setProperty( ConfigurationProperties.SERVER_LIST, (serverList == null) ? address : serverList + ";" + address); } catch (UnknownHostException e) { throw InfinispanMessages.MESSAGES.failedToInjectSocketBinding(e, value); } } }; dependencies.add( new Dependency<OutboundSocketBinding>( OutboundSocketBinding.OUTBOUND_SOCKET_BINDING_BASE_SERVICE_NAME.append( outboundSocketBinding), OutboundSocketBinding.class, injector)); } if (store.hasDefined(ModelKeys.CACHE)) { properties.setProperty("remoteCacheName", store.get(ModelKeys.CACHE).asString()); } if (store.hasDefined(ModelKeys.SOCKET_TIMEOUT)) { properties.setProperty( ConfigurationProperties.SO_TIMEOUT, store.require(ModelKeys.SOCKET_TIMEOUT).asString()); } if (store.hasDefined(ModelKeys.TCP_NO_DELAY)) { properties.setProperty( ConfigurationProperties.TCP_NO_DELAY, store.require(ModelKeys.TCP_NO_DELAY).asString()); } } else { String className = store.require(ModelKeys.CLASS).asString(); try { CacheLoader loader = CacheLoader.class .getClassLoader() .loadClass(className) .asSubclass(CacheLoader.class) .newInstance(); builder.cacheLoader(loader); } catch (Exception e) { throw new IllegalArgumentException( String.format("%s is not a valid cache store", className), e); } } }
/** * Create a Configuration object initialized from the operation ModelNode * * @param containerName the name of the cache container * @param cache ModelNode representing cache configuration * @param builder ConfigurationBuilder object to add data to * @return initialised Configuration object */ void processModelNode( OperationContext context, String containerName, ModelNode cache, ConfigurationBuilder builder, List<Dependency<?>> dependencies) throws OperationFailedException { final Indexing indexing = Indexing.valueOf( CommonAttributes.INDEXING.resolveModelAttribute(context, cache).asString()); final boolean batching = CommonAttributes.BATCHING.resolveModelAttribute(context, cache).asBoolean(); // set the cache mode (may be modified when setting up clustering attributes) builder.clustering().cacheMode(this.mode); final ModelNode indexingPropertiesModel = CommonAttributes.INDEXING_PROPERTIES.resolveModelAttribute(context, cache); Properties indexingProperties = new Properties(); if (indexing.isEnabled() && indexingPropertiesModel.isDefined()) { for (Property p : indexingPropertiesModel.asPropertyList()) { String value = p.getValue().asString(); indexingProperties.put(p.getName(), value); } } builder .indexing() .enabled(indexing.isEnabled()) .indexLocalOnly(indexing.isLocalOnly()) .withProperties(indexingProperties); // locking is a child resource if (cache.hasDefined(ModelKeys.LOCKING) && cache.get(ModelKeys.LOCKING, ModelKeys.LOCKING_NAME).isDefined()) { ModelNode locking = cache.get(ModelKeys.LOCKING, ModelKeys.LOCKING_NAME); final IsolationLevel isolationLevel = IsolationLevel.valueOf( CommonAttributes.ISOLATION.resolveModelAttribute(context, locking).asString()); final boolean striping = CommonAttributes.STRIPING.resolveModelAttribute(context, locking).asBoolean(); final long acquireTimeout = CommonAttributes.ACQUIRE_TIMEOUT.resolveModelAttribute(context, locking).asLong(); final int concurrencyLevel = CommonAttributes.CONCURRENCY_LEVEL.resolveModelAttribute(context, locking).asInt(); builder .locking() .isolationLevel(isolationLevel) .useLockStriping(striping) .lockAcquisitionTimeout(acquireTimeout) .concurrencyLevel(concurrencyLevel); } TransactionMode txMode = TransactionMode.NONE; LockingMode lockingMode = LockingMode.OPTIMISTIC; // locking is a child resource if (cache.hasDefined(ModelKeys.TRANSACTION) && cache.get(ModelKeys.TRANSACTION, ModelKeys.TRANSACTION_NAME).isDefined()) { ModelNode transaction = cache.get(ModelKeys.TRANSACTION, ModelKeys.TRANSACTION_NAME); final long stopTimeout = CommonAttributes.STOP_TIMEOUT.resolveModelAttribute(context, transaction).asLong(); txMode = TransactionMode.valueOf( CommonAttributes.MODE.resolveModelAttribute(context, transaction).asString()); lockingMode = LockingMode.valueOf( CommonAttributes.LOCKING.resolveModelAttribute(context, transaction).asString()); builder.transaction().cacheStopTimeout(stopTimeout); } builder .transaction() .transactionMode(txMode.getMode()) .lockingMode(lockingMode) .useSynchronization(!txMode.isXAEnabled()) .recovery() .enabled(txMode.isRecoveryEnabled()); if (txMode.isRecoveryEnabled()) { builder.transaction().syncCommitPhase(true).syncRollbackPhase(true); } if (batching) { builder .transaction() .transactionMode(org.infinispan.transaction.TransactionMode.TRANSACTIONAL) .invocationBatching() .enable(); } else { builder.transaction().invocationBatching().disable(); } // eviction is a child resource if (cache.hasDefined(ModelKeys.EVICTION) && cache.get(ModelKeys.EVICTION, ModelKeys.EVICTION_NAME).isDefined()) { ModelNode eviction = cache.get(ModelKeys.EVICTION, ModelKeys.EVICTION_NAME); final EvictionStrategy strategy = EvictionStrategy.valueOf( CommonAttributes.EVICTION_STRATEGY .resolveModelAttribute(context, eviction) .asString()); builder.eviction().strategy(strategy); if (strategy.isEnabled()) { final int maxEntries = CommonAttributes.MAX_ENTRIES.resolveModelAttribute(context, eviction).asInt(); builder.eviction().maxEntries(maxEntries); } } // expiration is a child resource if (cache.hasDefined(ModelKeys.EXPIRATION) && cache.get(ModelKeys.EXPIRATION, ModelKeys.EXPIRATION_NAME).isDefined()) { ModelNode expiration = cache.get(ModelKeys.EXPIRATION, ModelKeys.EXPIRATION_NAME); final long maxIdle = CommonAttributes.MAX_IDLE.resolveModelAttribute(context, expiration).asLong(); final long lifespan = CommonAttributes.LIFESPAN.resolveModelAttribute(context, expiration).asLong(); final long interval = CommonAttributes.INTERVAL.resolveModelAttribute(context, expiration).asLong(); builder.expiration().maxIdle(maxIdle).lifespan(lifespan).wakeUpInterval(interval); // Only enable the reaper thread if we need it if ((maxIdle > 0) || (lifespan > 0)) { builder.expiration().enableReaper(); } else { builder.expiration().disableReaper(); } } // stores are a child resource String storeKey = this.findStoreKey(cache); if (storeKey != null) { ModelNode store = this.getStoreModelNode(cache); final boolean shared = CommonAttributes.SHARED.resolveModelAttribute(context, store).asBoolean(); final boolean preload = CommonAttributes.PRELOAD.resolveModelAttribute(context, store).asBoolean(); final boolean passivation = CommonAttributes.PASSIVATION.resolveModelAttribute(context, store).asBoolean(); final boolean fetchState = CommonAttributes.FETCH_STATE.resolveModelAttribute(context, store).asBoolean(); final boolean purge = CommonAttributes.PURGE.resolveModelAttribute(context, store).asBoolean(); final boolean singleton = CommonAttributes.SINGLETON.resolveModelAttribute(context, store).asBoolean(); final boolean async = store.hasDefined(ModelKeys.WRITE_BEHIND) && store.get(ModelKeys.WRITE_BEHIND, ModelKeys.WRITE_BEHIND_NAME).isDefined(); builder.loaders().shared(shared).preload(preload).passivation(passivation); LoaderConfigurationBuilder storeBuilder = builder .loaders() .addCacheLoader() .fetchPersistentState(fetchState) .purgeOnStartup(purge) .purgeSynchronously(true); storeBuilder.singletonStore().enabled(singleton); if (async) { ModelNode writeBehind = store.get(ModelKeys.WRITE_BEHIND, ModelKeys.WRITE_BEHIND_NAME); storeBuilder .async() .enable() .flushLockTimeout( CommonAttributes.FLUSH_LOCK_TIMEOUT .resolveModelAttribute(context, writeBehind) .asLong()) .modificationQueueSize( CommonAttributes.MODIFICATION_QUEUE_SIZE .resolveModelAttribute(context, writeBehind) .asInt()) .shutdownTimeout( CommonAttributes.SHUTDOWN_TIMEOUT .resolveModelAttribute(context, writeBehind) .asLong()) .threadPoolSize( CommonAttributes.THREAD_POOL_SIZE .resolveModelAttribute(context, writeBehind) .asInt()); } this.buildCacheStore(context, storeBuilder, containerName, store, storeKey, dependencies); } }
/** * Create a Configuration object initialized from the operation ModelNode * * @param cache ModelNode representing cache configuration * @param builder ConfigurationBuilder object to add data to * @return initialised Configuration object */ void processModelNode( ModelNode cache, ConfigurationBuilder builder, List<Dependency<?>> dependencies) { String cacheName = cache.require(ModelKeys.NAME).asString(); builder.classLoader(this.getClass().getClassLoader()); builder .clustering() .cacheMode(CacheMode.valueOf(cache.require(ModelKeys.CACHE_MODE).asString())); if (cache.hasDefined(ModelKeys.INDEXING)) { Indexing indexing = Indexing.valueOf(cache.get(ModelKeys.INDEXING).asString()); builder.indexing().enabled(indexing.isEnabled()).indexLocalOnly(indexing.isLocalOnly()); } if (cache.hasDefined(ModelKeys.QUEUE_SIZE)) { int size = cache.get(ModelKeys.QUEUE_SIZE).asInt(); builder.clustering().async().replQueueMaxElements(size).useReplQueue(size > 0); } if (cache.hasDefined(ModelKeys.QUEUE_FLUSH_INTERVAL)) { builder .clustering() .async() .replQueueInterval(cache.get(ModelKeys.QUEUE_FLUSH_INTERVAL).asLong()); } if (cache.hasDefined(ModelKeys.REMOTE_TIMEOUT)) { builder.clustering().sync().replTimeout(cache.get(ModelKeys.REMOTE_TIMEOUT).asLong()); } if (cache.hasDefined(ModelKeys.OWNERS)) { builder.clustering().hash().numOwners(cache.get(ModelKeys.OWNERS).asInt()); } if (cache.hasDefined(ModelKeys.VIRTUAL_NODES)) { builder.clustering().hash().numVirtualNodes(cache.get(ModelKeys.VIRTUAL_NODES).asInt()); } if (cache.hasDefined(ModelKeys.L1_LIFESPAN)) { long lifespan = cache.get(ModelKeys.L1_LIFESPAN).asLong(); if (lifespan > 0) { builder.clustering().l1().enable().lifespan(lifespan); } else { builder.clustering().l1().disable(); } } // locking is a child resource if (cache.hasDefined(ModelKeys.LOCKING) && cache.get(ModelKeys.LOCKING, ModelKeys.LOCKING_NAME).isDefined()) { ModelNode locking = cache.get(ModelKeys.LOCKING, ModelKeys.LOCKING_NAME); if (locking.hasDefined(ModelKeys.ISOLATION)) { builder .locking() .isolationLevel(IsolationLevel.valueOf(locking.get(ModelKeys.ISOLATION).asString())); } if (locking.hasDefined(ModelKeys.STRIPING)) { builder.locking().useLockStriping(locking.get(ModelKeys.STRIPING).asBoolean()); } if (locking.hasDefined(ModelKeys.ACQUIRE_TIMEOUT)) { builder.locking().lockAcquisitionTimeout(locking.get(ModelKeys.ACQUIRE_TIMEOUT).asLong()); } if (locking.hasDefined(ModelKeys.CONCURRENCY_LEVEL)) { builder.locking().concurrencyLevel(locking.get(ModelKeys.CONCURRENCY_LEVEL).asInt()); } } TransactionMode txMode = TransactionMode.NONE; LockingMode lockingMode = LockingMode.OPTIMISTIC; // locking is a child resource if (cache.hasDefined(ModelKeys.TRANSACTION) && cache.get(ModelKeys.TRANSACTION, ModelKeys.TRANSACTION_NAME).isDefined()) { ModelNode transaction = cache.get(ModelKeys.TRANSACTION, ModelKeys.TRANSACTION_NAME); if (transaction.hasDefined(ModelKeys.STOP_TIMEOUT)) { builder.transaction().cacheStopTimeout(transaction.get(ModelKeys.STOP_TIMEOUT).asLong()); } if (transaction.hasDefined(ModelKeys.MODE)) { txMode = TransactionMode.valueOf(transaction.get(ModelKeys.MODE).asString()); } if (transaction.hasDefined(ModelKeys.LOCKING)) { lockingMode = LockingMode.valueOf(transaction.get(ModelKeys.LOCKING).asString()); } } builder .transaction() .transactionMode(txMode.getMode()) .lockingMode(lockingMode) .useSynchronization(!txMode.isXAEnabled()) .recovery() .enabled(txMode.isRecoveryEnabled()); if (txMode.isRecoveryEnabled()) { builder.transaction().syncCommitPhase(true).syncRollbackPhase(true); } if (cache.hasDefined(ModelKeys.BATCHING)) { InvocationBatchingConfigurationBuilder batchingBuilder = builder .transaction() .transactionMode(org.infinispan.transaction.TransactionMode.TRANSACTIONAL) .invocationBatching(); if (cache.get(ModelKeys.BATCHING).asBoolean()) { batchingBuilder.enable(); } else { batchingBuilder.disable(); } } // eviction is a child resource if (cache.hasDefined(ModelKeys.EVICTION) && cache.get(ModelKeys.EVICTION, ModelKeys.EVICTION_NAME).isDefined()) { ModelNode eviction = cache.get(ModelKeys.EVICTION, ModelKeys.EVICTION_NAME); if (eviction.hasDefined(ModelKeys.STRATEGY)) { builder .eviction() .strategy(EvictionStrategy.valueOf(eviction.get(ModelKeys.STRATEGY).asString())); } if (eviction.hasDefined(ModelKeys.MAX_ENTRIES)) { builder.eviction().maxEntries(eviction.get(ModelKeys.MAX_ENTRIES).asInt()); } } // expiration is a child resource if (cache.hasDefined(ModelKeys.EXPIRATION) && cache.get(ModelKeys.EXPIRATION, ModelKeys.EXPIRATION_NAME).isDefined()) { ModelNode expiration = cache.get(ModelKeys.EXPIRATION, ModelKeys.EXPIRATION_NAME); if (expiration.hasDefined(ModelKeys.MAX_IDLE)) { builder.expiration().maxIdle(expiration.get(ModelKeys.MAX_IDLE).asLong()); } if (expiration.hasDefined(ModelKeys.LIFESPAN)) { builder.expiration().lifespan(expiration.get(ModelKeys.LIFESPAN).asLong()); } if (expiration.hasDefined(ModelKeys.INTERVAL)) { builder.expiration().wakeUpInterval(expiration.get(ModelKeys.INTERVAL).asLong()); } } String storeKey = this.findStoreKey(cache); if (storeKey != null) { ModelNode store = this.getStoreModelNode(cache); builder .loaders() .shared( store.hasDefined(ModelKeys.SHARED) ? store.get(ModelKeys.SHARED).asBoolean() : false) .preload( store.hasDefined(ModelKeys.PRELOAD) ? store.get(ModelKeys.PRELOAD).asBoolean() : false) .passivation( store.hasDefined(ModelKeys.PASSIVATION) ? store.get(ModelKeys.PASSIVATION).asBoolean() : true); LoaderConfigurationBuilder storeBuilder = builder .loaders() .addCacheLoader() .fetchPersistentState( store.hasDefined(ModelKeys.FETCH_STATE) ? store.get(ModelKeys.FETCH_STATE).asBoolean() : true) .purgeOnStartup( store.hasDefined(ModelKeys.PURGE) ? store.get(ModelKeys.PURGE).asBoolean() : true) .purgeSynchronously(true); storeBuilder .singletonStore() .enabled( store.hasDefined(ModelKeys.SINGLETON) ? store.get(ModelKeys.SINGLETON).asBoolean() : false); this.buildCacheStore(storeBuilder, cacheName, store, storeKey, dependencies); } }