KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > vendor > jetty > BindPrimroseFactory


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */

21
22 /** Jetty Service implementation code taken from the JettyPlus codebase JotmService.java
23     Jetty license below :
24 **/

25 // ========================================================================
26
// $Id: JotmService.java,v 1.10 2005/03/30 18:20:08 janb Exp $
27
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
28
// ------------------------------------------------------------------------
29
// Licensed under the Apache License, Version 2.0 (the "License");
30
// you may not use this file except in compliance with the License.
31
// You may obtain a copy of the License at
32
// http://www.apache.org/licenses/LICENSE-2.0
33
// Unless required by applicable law or agreed to in writing, software
34
// distributed under the License is distributed on an "AS IS" BASIS,
35
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36
// See the License for the specific language governing permissions and
37
// limitations under the License.
38
// ========================================================================
39

40 package uk.org.primrose.vendor.jetty;
41
42 import java.util.ArrayList JavaDoc;
43 import javax.naming.Reference JavaDoc;
44 import javax.naming.StringRefAddr JavaDoc;
45 import uk.org.primrose.pool.datasource.MasterPoolDataSourceFactory;
46 import uk.org.primrose.pool.jmx.MBeanUtil;
47
48 // import stuff needed for Jetty
49
import org.mortbay.jetty.plus.*;
50 import javax.transaction.TransactionManager JavaDoc;
51 import javax.transaction.UserTransaction JavaDoc;
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54
55 public class BindPrimroseFactory extends TMService {
56     private String JavaDoc poolConfigFile = null;
57     private ArrayList JavaDoc pools = null;
58
59     // log startup to Jetty
60
private static Log log = LogFactory.getLog(BindPrimroseFactory.class);
61     public static final String JavaDoc DEFAULT_SERVICE_NAME = "PrimroseService";
62
63
64     public void setPoolConfigFile(String JavaDoc poolConfigFile) {
65         if (this.poolConfigFile == null) {
66             initialize(poolConfigFile);
67         }
68
69         this.poolConfigFile = poolConfigFile;
70     }
71
72     public String JavaDoc getPoolConfigFile() {
73         return this.poolConfigFile;
74     }
75
76     public void initialize(String JavaDoc poolConfigFile) {
77             // Initialize the pool and bind all objects under the non-serializable
78
try {
79                 log.info("Primrose binding using config file : " +poolConfigFile);
80                 Reference JavaDoc ref = new Reference JavaDoc("uk.org.primrose.pool.datasource.MasterPoolDataSourceFactory");
81                 String JavaDoc refType = "configFile";
82                 String JavaDoc refAddr = poolConfigFile;
83                 StringRefAddr JavaDoc sra = new StringRefAddr JavaDoc(refType, refAddr);
84                 ref.add(sra);
85                 MasterPoolDataSourceFactory mpdsf = new MasterPoolDataSourceFactory();
86                 // Bind the masterPool under the tree.
87
Object JavaDoc o = mpdsf.getObjectInstance(ref, null, null, null);
88                 this.pools = (ArrayList JavaDoc)o;
89
90                 MBeanUtil.bind("masterPool", this);
91             } catch (Exception JavaDoc e) {
92                 e.printStackTrace(System.out);
93             }
94
95             log.info("Primrose started.");
96     }
97
98     public ArrayList JavaDoc getPools() {
99         return pools;
100     }
101
102
103
104
105     // Jetty Service implementation code
106

107    /**
108      * Instance of JOTM transaction manager.
109      */

110     protected org.objectweb.transaction.jta.TMService m_tm;
111
112
113     public BindPrimroseFactory() {
114         m_tm = null;
115     }
116
117
118     /**
119      * returns a <code>TransactionManager</code> object.
120      *
121      * @return TransactionManager
122      */

123     public TransactionManager JavaDoc getTransactionManager()
124     {
125        if (m_tm == null)
126        {
127           return null;
128        }
129        else
130        {
131           return m_tm.getTransactionManager();
132        }
133     }
134
135     /**
136      * Returns an <code>UserTransaction</code> object.
137      *
138      * @return UserTransaction
139      */

140     public UserTransaction JavaDoc getUserTransaction()
141     {
142        if (m_tm == null)
143        {
144           return null;
145        }
146        else
147        {
148           return m_tm.getUserTransaction();
149        }
150     }
151
152
153     /* ------------------------------------------------------------ */
154     /** Start the LifeCycle.
155      * @exception Exception An arbitrary exception may be thrown.
156      */

157     public void start() throws Exception JavaDoc {
158         super.start();
159     }
160
161     /* ------------------------------------------------------------ */
162     /** Stop the LifeCycle.
163      * The LifeCycle may wait for current activities to complete
164      * normally, but it can be interrupted.
165      * @exception InterruptedException Stopping a lifecycle is rarely atomic
166      * and may be interrupted by another thread. If this happens
167      * InterruptedException is throw and the component will be in an
168      * indeterminant state and should probably be discarded.
169      */

170     public void stop() throws InterruptedException JavaDoc {
171         super.stop();
172         log.info("Primrose stopped.");
173     }
174
175
176 }
Popular Tags