diff --git a/modules/client/src/com/haulmont/cuba/client/sys/ConfigurationClientImpl.java b/modules/client/src/com/haulmont/cuba/client/sys/ConfigurationClientImpl.java index c9e26022d2..b7b2b40aef 100644 --- a/modules/client/src/com/haulmont/cuba/client/sys/ConfigurationClientImpl.java +++ b/modules/client/src/com/haulmont/cuba/client/sys/ConfigurationClientImpl.java @@ -21,9 +21,10 @@ import com.haulmont.cuba.client.ClientConfiguration; import com.haulmont.cuba.client.sys.config.ConfigPersisterClientImpl; import com.haulmont.cuba.core.config.Config; import com.haulmont.cuba.core.config.ConfigHandler; +import com.haulmont.cuba.core.config.ConfigPersister; import com.haulmont.cuba.core.global.Configuration; - import org.springframework.stereotype.Component; + import java.lang.reflect.Proxy; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -37,7 +38,7 @@ public class ConfigurationClientImpl implements ClientConfiguration { public T getConfig(Class configInterface) { ConfigHandler handler = handlersCache.get(configInterface); if (handler == null) { - handler = new ConfigHandler(new ConfigPersisterClientImpl(false), configInterface); + handler = new ConfigHandler(createConfigPersister(false), configInterface); handlersCache.put(configInterface, handler); } ClassLoader classLoader = configInterface.getClassLoader(); @@ -47,8 +48,12 @@ public class ConfigurationClientImpl implements ClientConfiguration { @Override public T getConfigCached(Class configInterface) { - ConfigHandler handler = new ConfigHandler(new ConfigPersisterClientImpl(true), configInterface); + ConfigHandler handler = new ConfigHandler(createConfigPersister(true), configInterface); Object proxy = Proxy.newProxyInstance(configInterface.getClassLoader(), new Class[]{configInterface}, handler); return configInterface.cast(proxy); } + + protected ConfigPersister createConfigPersister(boolean caching) { + return new ConfigPersisterClientImpl(caching); + } } \ No newline at end of file diff --git a/modules/desktop/src/com/haulmont/cuba/desktop/sys/config/DesktopConfigurationImpl.java b/modules/desktop/src/com/haulmont/cuba/desktop/sys/config/DesktopConfigurationImpl.java index 05e2d37ce8..bf2d0d2d68 100644 --- a/modules/desktop/src/com/haulmont/cuba/desktop/sys/config/DesktopConfigurationImpl.java +++ b/modules/desktop/src/com/haulmont/cuba/desktop/sys/config/DesktopConfigurationImpl.java @@ -18,41 +18,20 @@ package com.haulmont.cuba.desktop.sys.config; import com.haulmont.cuba.client.ClientConfiguration; -import com.haulmont.cuba.core.config.Config; -import com.haulmont.cuba.core.config.ConfigHandler; - -import java.lang.reflect.Proxy; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import com.haulmont.cuba.client.sys.ConfigurationClientImpl; +import com.haulmont.cuba.core.config.ConfigPersister; /** * Desktop specific implementation of Configuration that uses application scope cache for db properties. * * @see com.haulmont.cuba.desktop.sys.config.DesktopConfigStorageCache - * */ -public class DesktopConfigurationImpl implements ClientConfiguration { - - protected Map handlersCache = new ConcurrentHashMap<>(); +public class DesktopConfigurationImpl extends ConfigurationClientImpl implements ClientConfiguration { protected DesktopConfigStorageCache configStorageCache = new DesktopConfigStorageCache(); @Override - public T getConfig(Class configInterface) { - ConfigHandler handler = handlersCache.get(configInterface); - if (handler == null) { - handler = new ConfigHandler(new DesktopConfigPersisterImpl(configStorageCache, false), configInterface); - handlersCache.put(configInterface, handler); - } - ClassLoader classLoader = configInterface.getClassLoader(); - Object proxy = Proxy.newProxyInstance(classLoader, new Class[]{configInterface}, handler); - return configInterface.cast(proxy); - } - - @Override - public T getConfigCached(Class configInterface) { - ConfigHandler handler = new ConfigHandler(new DesktopConfigPersisterImpl(configStorageCache, true), configInterface); - Object proxy = Proxy.newProxyInstance(configInterface.getClassLoader(), new Class[]{configInterface}, handler); - return configInterface.cast(proxy); + protected ConfigPersister createConfigPersister(boolean caching) { + return new DesktopConfigPersisterImpl(configStorageCache, caching); } } \ No newline at end of file diff --git a/modules/web/src/com/haulmont/cuba/web/cache/WebConfigCacheStrategy.java b/modules/web/src/com/haulmont/cuba/web/cache/WebConfigCacheStrategy.java new file mode 100644 index 0000000000..81f9979607 --- /dev/null +++ b/modules/web/src/com/haulmont/cuba/web/cache/WebConfigCacheStrategy.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2008-2016 Haulmont. + * + * 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.haulmont.cuba.web.cache; + +import com.haulmont.cuba.client.sys.cache.CachingStrategy; +import com.haulmont.cuba.core.app.ConfigStorageService; +import org.springframework.stereotype.Component; + +import javax.inject.Inject; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +@Component(WebConfigCacheStrategy.NAME) +public class WebConfigCacheStrategy implements CachingStrategy { + public static final String NAME = "cuba_WebConfigCacheStrategy"; + + protected volatile Map cachedProperties = Collections.unmodifiableMap(new HashMap<>()); + + @Inject + protected ConfigStorageService configStorageService; + + protected volatile long lastUsedTs = 0; + protected volatile long updateIntervalMs = 60 * 1000; + + protected ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); + + @Override + public void init() { + } + + @Override + public Object getObject() { + return cachedProperties; + } + + @Override + public Object loadObject() { + Map cachedPropertiesFromServer = Collections.unmodifiableMap(configStorageService.getDbProperties()); + + cachedProperties = cachedPropertiesFromServer; + lastUsedTs = System.currentTimeMillis(); + + return cachedPropertiesFromServer; + } + + @Override + public ReadWriteLock lock() { + return readWriteLock; + } + + @Override + public boolean needToReload() { + return System.currentTimeMillis() - lastUsedTs > updateIntervalMs; + } + + public long getUpdateIntervalMs() { + return updateIntervalMs; + } + + public void setUpdateIntervalMs(long updateIntervalMs) { + this.updateIntervalMs = updateIntervalMs; + } +} \ No newline at end of file diff --git a/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigStorageCache.java b/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigStorageCache.java new file mode 100644 index 0000000000..fc38d6d75f --- /dev/null +++ b/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigStorageCache.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2008-2016 Haulmont. + * + * 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.haulmont.cuba.web.sys.config; + +import com.haulmont.cuba.client.sys.cache.ClientCacheManager; +import com.haulmont.cuba.core.app.ConfigStorageService; +import com.haulmont.cuba.core.config.AppPropertyEntity; +import com.haulmont.cuba.core.global.AppBeans; +import com.haulmont.cuba.web.cache.WebConfigCacheStrategy; + +import java.util.List; +import java.util.Map; + +public class WebConfigStorageCache implements ConfigStorageService { + @Override + public Map getDbProperties() { + return getClientCacheManager().getCached(WebConfigCacheStrategy.NAME); + } + + @Override + public String getDbProperty(String name) { + return getDbProperties().get(name); + } + + @Override + public void setDbProperty(String name, String value) { + getService().setDbProperty(name, value); + + getClientCacheManager().refreshCached(WebConfigStorageCache.NAME); + } + + protected ConfigStorageService getService() { + return AppBeans.get(ConfigStorageService.NAME); + } + + protected ClientCacheManager getClientCacheManager() { + return AppBeans.get(ClientCacheManager.NAME); + } + + @Override + public List getAppProperties() { + return getService().getAppProperties(); + } +} \ No newline at end of file diff --git a/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigStoragePersisterImpl.java b/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigStoragePersisterImpl.java new file mode 100644 index 0000000000..dcdcffc15d --- /dev/null +++ b/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigStoragePersisterImpl.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2008-2016 Haulmont. + * + * 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.haulmont.cuba.web.sys.config; + +import com.haulmont.cuba.client.sys.config.ConfigPersisterClientImpl; +import com.haulmont.cuba.core.app.ConfigStorageService; + +public class WebConfigStoragePersisterImpl extends ConfigPersisterClientImpl { + private WebConfigStorageCache configStorageCache; + + public WebConfigStoragePersisterImpl(WebConfigStorageCache configStorageCache, boolean caching) { + super(caching); + this.configStorageCache = configStorageCache; + } + + @Override + protected ConfigStorageService getConfigStorage() { + return configStorageCache; + } +} \ No newline at end of file diff --git a/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigurationImpl.java b/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigurationImpl.java new file mode 100644 index 0000000000..7505beb846 --- /dev/null +++ b/modules/web/src/com/haulmont/cuba/web/sys/config/WebConfigurationImpl.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2008-2016 Haulmont. + * + * 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.haulmont.cuba.web.sys.config; + +import com.haulmont.cuba.client.ClientConfiguration; +import com.haulmont.cuba.client.sys.ConfigurationClientImpl; +import com.haulmont.cuba.core.config.ConfigPersister; + +public class WebConfigurationImpl extends ConfigurationClientImpl implements ClientConfiguration { + protected WebConfigStorageCache configStorageCache = new WebConfigStorageCache(); + + @Override + protected ConfigPersister createConfigPersister(boolean caching) { + return new WebConfigStoragePersisterImpl(configStorageCache, caching); + } +} \ No newline at end of file diff --git a/modules/web/src/cuba-web-spring.xml b/modules/web/src/cuba-web-spring.xml index 391953c588..01e506c4fc 100644 --- a/modules/web/src/cuba-web-spring.xml +++ b/modules/web/src/cuba-web-spring.xml @@ -40,6 +40,8 @@ + +