KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > cluster > PersistentStoreConfig


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.cluster;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.log.Log;
34 import com.caucho.naming.Jndi;
35 import com.caucho.util.L10N;
36
37 import javax.annotation.PostConstruct;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Configuration distributed stores.
43  */

44 public class PersistentStoreConfig {
45   static protected final Logger JavaDoc log = Log.open(PersistentStoreConfig.class);
46   static final L10N L = new L10N(PersistentStoreConfig.class);
47
48   private String JavaDoc _name = "caucho/persistent-store";
49
50   private StoreManager _store;
51
52   /**
53    * Sets the persistent store name.
54    */

55   public void setJndiName(String JavaDoc name)
56   {
57     _name = name;
58   }
59   
60   /**
61    * Sets the persistent store type.
62    */

63   public void setType(String JavaDoc type)
64     throws ConfigException
65   {
66     Cluster cluster = Cluster.getLocal();
67     
68     if (type.equals("jdbc")) {
69       try {
70     Class JavaDoc cl = Class.forName("com.caucho.server.cluster.JdbcStore");
71     
72     _store = (StoreManager) cl.newInstance();
73
74     if (cluster != null)
75       cluster.setStore(_store);
76       } catch (Throwable JavaDoc e) {
77     log.log(Level.FINER, e.toString(), e);
78       }
79
80       if (_store == null)
81     throw new ConfigException(L.l("'{0}' persistent sessions are available in Resin Professional. See http://www.caucho.com for information and licensing.",
82                       type));
83     }
84     else if (type.equals("file"))
85       _store = new FileStore();
86     else if (type.equals("cluster") || type.equals("tcp")) {
87       if (cluster == null)
88     throw new ConfigException(L.l("Cluster store needs a defined <cluster>. Use 'file' for single-machine persistence."));
89       
90       try {
91     Class JavaDoc cl = Class.forName("com.caucho.server.cluster.ClusterStore");
92
93     _store = (StoreManager) cl.newInstance();
94
95     if (cluster != null)
96       cluster.setStore(_store);
97       } catch (Throwable JavaDoc e) {
98     log.log(Level.FINER, e.toString(), e);
99       }
100
101       if (_store == null)
102     throw new ConfigException(L.l("'{0}' persistent sessions are available in Resin Professional. See http://www.caucho.com for information and licensing.",
103                       type));
104     }
105
106     if (_store == null)
107       throw new ConfigException(L.l("{0} is an unknown persistent-store type. Only 'jdbc', 'file', and 'tcp' are allowed.",
108                     type));
109   }
110
111   public StoreManager createInit()
112   {
113     return _store;
114   }
115
116   @PostConstruct
117   public void init()
118     throws Exception JavaDoc
119   {
120     if (_store == null)
121       throw new ConfigException(L.l("type is a required attribute of persistent-store"));
122
123     _store.init();
124
125     if (_name.startsWith("java:comp"))
126       Jndi.bindDeep(_name, _store);
127     else
128       Jndi.bindDeep("java:comp/env/" + _name, _store);
129   }
130 }
131
Popular Tags