001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.cluster;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.log.Log;
034: import com.caucho.naming.Jndi;
035: import com.caucho.util.L10N;
036:
037: import javax.annotation.PostConstruct;
038: import java.util.logging.Level;
039: import java.util.logging.Logger;
040:
041: /**
042: * Configuration distributed stores.
043: */
044: public class PersistentStoreConfig {
045: static protected final Logger log = Log
046: .open(PersistentStoreConfig.class);
047: static final L10N L = new L10N(PersistentStoreConfig.class);
048:
049: private String _name = "caucho/persistent-store";
050:
051: private StoreManager _store;
052:
053: /**
054: * Sets the persistent store name.
055: */
056: public void setJndiName(String name) {
057: _name = name;
058: }
059:
060: /**
061: * Sets the persistent store type.
062: */
063: public void setType(String type) throws ConfigException {
064: Cluster cluster = Cluster.getLocal();
065:
066: if (type.equals("jdbc")) {
067: try {
068: Class cl = Class
069: .forName("com.caucho.server.cluster.JdbcStore");
070:
071: _store = (StoreManager) cl.newInstance();
072:
073: if (cluster != null)
074: cluster.setStore(_store);
075: } catch (Throwable e) {
076: log.log(Level.FINER, e.toString(), e);
077: }
078:
079: if (_store == null)
080: throw new ConfigException(
081: L
082: .l(
083: "'{0}' persistent sessions are available in Resin Professional. See http://www.caucho.com for information and licensing.",
084: type));
085: } else if (type.equals("file"))
086: _store = new FileStore();
087: else if (type.equals("cluster") || type.equals("tcp")) {
088: if (cluster == null)
089: throw new ConfigException(
090: L
091: .l("Cluster store needs a defined <cluster>. Use 'file' for single-machine persistence."));
092:
093: try {
094: Class cl = Class
095: .forName("com.caucho.server.cluster.ClusterStore");
096:
097: _store = (StoreManager) cl.newInstance();
098:
099: if (cluster != null)
100: cluster.setStore(_store);
101: } catch (Throwable e) {
102: log.log(Level.FINER, e.toString(), e);
103: }
104:
105: if (_store == null)
106: throw new ConfigException(
107: L
108: .l(
109: "'{0}' persistent sessions are available in Resin Professional. See http://www.caucho.com for information and licensing.",
110: type));
111: }
112:
113: if (_store == null)
114: throw new ConfigException(
115: L
116: .l(
117: "{0} is an unknown persistent-store type. Only 'jdbc', 'file', and 'tcp' are allowed.",
118: type));
119: }
120:
121: public StoreManager createInit() {
122: return _store;
123: }
124:
125: @PostConstruct
126: public void init() throws Exception {
127: if (_store == null)
128: throw new ConfigException(
129: L
130: .l("type is a required attribute of persistent-store"));
131:
132: _store.init();
133:
134: if (_name.startsWith("java:comp"))
135: Jndi.bindDeep(_name, _store);
136: else
137: Jndi.bindDeep("java:comp/env/" + _name, _store);
138: }
139: }
|