1 5 package org.exoplatform.services.config.impl; 6 7 import java.util.* ; 8 import org.exoplatform.container.configuration.* ; 9 import org.exoplatform.services.database.HibernateService; 10 import org.exoplatform.services.config.ConfigurationService; 11 import com.thoughtworks.xstream.XStream; 12 import com.thoughtworks.xstream.io.xml.XppDriver; 13 18 public class ConfigurationServiceImpl implements ConfigurationService { 19 static private String [] MAPPING = 20 { 21 "org/exoplatform/services/config/impl/ConfigurationDataImpl.hbm.xml" 22 } ; 23 24 private ConfigurationManager manager_ ; 25 private HibernateService hservice_ ; 26 private XStream xstream_ ; 27 28 public ConfigurationServiceImpl(ConfigurationManager manager, 29 HibernateService hservice) { 30 manager_ = manager ; 31 hservice_ = hservice ; 32 hservice_.addMappingFiles(MAPPING) ; 33 xstream_ = new XStream(new XppDriver()); 34 } 35 36 public Object getServiceConfiguration(Class serviceType) throws Exception { 37 ConfigurationDataImpl impl = 38 (ConfigurationDataImpl) hservice_.findOne(ConfigurationDataImpl.class, serviceType.getName()) ; 39 Object obj = null ; 40 if(impl == null) { 41 obj = loadDefaultConfig(serviceType) ; 42 saveServiceConfiguration(serviceType, obj) ; 43 } else { 44 obj = xstream_.fromXML(impl.getData()) ; 45 } 46 return obj; 47 } 48 49 public void saveServiceConfiguration(Class serviceType, Object config) throws Exception { 50 ConfigurationDataImpl impl = 51 (ConfigurationDataImpl) hservice_.findOne(ConfigurationDataImpl.class, serviceType.getName()) ; 52 String xml = xstream_.toXML(config) ; 53 if(impl == null) { 54 impl = new ConfigurationDataImpl(); 55 impl.setServiceType(serviceType.getName()) ; 56 impl.setData(xml) ; 57 hservice_.create(impl) ; 58 } else { 59 impl.setData(xml) ; 60 hservice_.update(impl) ; 61 } 62 } 63 64 public void removeServiceConfiguration(Class serviceType) throws Exception { 65 hservice_.remove(serviceType, serviceType.getName()) ; 66 } 67 68 private Object loadDefaultConfig(Class serviceType) throws Exception { 69 ServiceConfiguration sconf = manager_.getServiceConfiguration(serviceType) ; 70 Iterator i = sconf.values().iterator() ; 71 ObjectParam param = (ObjectParam) i.next() ; 72 return param.getObject() ; 73 } 74 } 75 | Popular Tags |