KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > config > impl > ConfigurationServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

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 /**
14  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
15  * @since Dec 5, 2004
16  * @version $Id$
17  */

18 public class ConfigurationServiceImpl implements ConfigurationService {
19   static private String JavaDoc[] 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 JavaDoc getServiceConfiguration(Class JavaDoc serviceType) throws Exception JavaDoc {
37     ConfigurationDataImpl impl =
38       (ConfigurationDataImpl) hservice_.findOne(ConfigurationDataImpl.class, serviceType.getName()) ;
39     Object JavaDoc 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 JavaDoc serviceType, Object JavaDoc config) throws Exception JavaDoc {
50     ConfigurationDataImpl impl =
51       (ConfigurationDataImpl) hservice_.findOne(ConfigurationDataImpl.class, serviceType.getName()) ;
52     String JavaDoc 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 JavaDoc serviceType) throws Exception JavaDoc {
65     hservice_.remove(serviceType, serviceType.getName()) ;
66   }
67   
68   private Object JavaDoc loadDefaultConfig(Class JavaDoc serviceType) throws Exception JavaDoc {
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