KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > components > controller > ControllerProcess


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): Emmanuel Cecchet.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.components.controller;
26
27 import java.io.File JavaDoc;
28
29 import org.objectweb.cjdbc.common.util.Constants;
30 import org.objectweb.cjdbc.controller.core.Controller;
31 import org.objectweb.cjdbc.controller.core.ControllerConstants;
32 import org.objectweb.cjdbc.controller.core.ControllerFactory;
33 import org.objectweb.cjdbc.scenario.tools.ScenarioConstants;
34 import org.objectweb.cjdbc.scenario.tools.components.ComponentInterface;
35
36 /**
37  * This class defines a ControllerProcess
38  *
39  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
40  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
41  * </a>
42  * @version 1.0
43  */

44 public class ControllerProcess implements ComponentInterface
45 {
46   String JavaDoc port;
47   String JavaDoc database;
48   Controller controller;
49
50   /**
51    * Creates a new <code>ControllerProcess</code> object
52    *
53    * @param port port use by the controller
54    * @param database database to load with this controller
55    * @throws Exception if fails
56    */

57   public ControllerProcess(String JavaDoc port, String JavaDoc database) throws Exception JavaDoc
58   {
59     this.port = port;
60     this.database = database;
61     start();
62   }
63
64   /**
65    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#start()
66    */

67   public void start() throws Exception JavaDoc
68   {
69     String JavaDoc controllerConfig = getClass().getResource(
70         ScenarioConstants.CONTROLLER_DIR
71             + ScenarioConstants.CONTROLLER_DEFAULT_FILE).getPath();
72     String JavaDoc[] args = new String JavaDoc[]{"-p", port, "-f", controllerConfig};
73     ControllerFactory factory = new ControllerFactory(args);
74     Controller controller = factory.getController();
75     controller.setConfiguration(factory);
76     if (controller != null)
77       controller.launch();
78     this.controller = controller;
79   }
80
81   /**
82    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#getDatabase()
83    */

84   public String JavaDoc getDatabase()
85   {
86     return database;
87   }
88
89   /**
90    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#loadDatabase(java.lang.String)
91    */

92   public void loadDatabase(String JavaDoc xml) throws Exception JavaDoc
93   {
94     this.loadDatabase(xml, ScenarioConstants.DEFAULT_VDB_NAME);
95   }
96
97   /**
98    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#loadDatabase(java.lang.String,
99    * java.lang.String)
100    */

101   public void loadDatabase(String JavaDoc xml, String JavaDoc targetDB) throws Exception JavaDoc
102   {
103     File JavaDoc f = new File JavaDoc(xml);
104     if (!f.exists())
105     {
106       try
107       {
108         f = new File JavaDoc(getClass().getResource(
109             ScenarioConstants.VIRTUALDATABASE_DIR + File.separator + xml)
110             .getFile());
111       }
112       catch (Exception JavaDoc e)
113       {
114         throw new Exception JavaDoc("could not find configuration file:" + xml);
115       }
116       if (!f.exists())
117         throw new Exception JavaDoc("could not find configuration file:" + xml);
118     }
119     if (!f.exists())
120       throw new Exception JavaDoc("File configuration not found");
121     controller.loadXmlConfiguration(f.getAbsolutePath(), targetDB,
122         ControllerConstants.AUTO_ENABLE_TRUE, "");
123     if (controller.hasVirtualDatabase(targetDB) == false)
124       throw new Exception JavaDoc("Virtual Database Configuration failed");
125   }
126
127   /**
128    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#loadDatabase()
129    */

130   public void loadDatabase() throws Exception JavaDoc
131   {
132     loadDatabase(ScenarioConstants.DATABASE_CONFIG_FILE);
133   }
134
135   /**
136    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#getPort()
137    */

138   public String JavaDoc getPort()
139   {
140     return String.valueOf(controller.getPortNumber());
141   }
142
143   /**
144    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#getProcess()
145    */

146   public Object JavaDoc getProcess()
147   {
148     return controller;
149   }
150
151   /**
152    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentInterface#release()
153    */

154   public void release()
155   {
156     try
157     {
158       controller.shutdown(Constants.SHUTDOWN_SAFE);
159     }
160     catch (Exception JavaDoc e)
161     {
162       e.printStackTrace();
163     }
164   }
165
166 }
Popular Tags