1. /*
  2. * @(#)RepositoryImpl.java 1.45 04/06/21
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * @(#)RepositoryImpl.java 1.1 97/10/17
  9. *
  10. * Copyright 1993-1997 Sun Microsystems, Inc. 901 San Antonio Road,
  11. * Palo Alto, California, 94303, U.S.A. All Rights Reserved.
  12. *
  13. * This software is the confidential and proprietary information of Sun
  14. * Microsystems, Inc. ("Confidential Information"). You shall not
  15. * disclose such Confidential Information and shall use it only in
  16. * accordance with the terms of the license agreement you entered into
  17. * with Sun.
  18. *
  19. * CopyrightVersion 1.2
  20. *
  21. */
  22. package com.sun.corba.se.impl.activation;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26. import java.io.ObjectInputStream;
  27. import java.io.ObjectOutputStream;
  28. import java.io.Serializable;
  29. import java.util.Properties;
  30. import java.util.Hashtable;
  31. import java.util.Enumeration;
  32. import java.util.Vector;
  33. import org.omg.CORBA.CompletionStatus;
  34. import org.omg.CORBA.INITIALIZE;
  35. import org.omg.CORBA.INTERNAL;
  36. import org.omg.CORBA.SystemException;
  37. import com.sun.corba.se.spi.activation.BadServerDefinition;
  38. import com.sun.corba.se.spi.activation.RepositoryPackage.ServerDef;
  39. import com.sun.corba.se.spi.activation._RepositoryImplBase;
  40. import com.sun.corba.se.spi.activation.ServerAlreadyRegistered;
  41. import com.sun.corba.se.spi.activation.ServerAlreadyInstalled;
  42. import com.sun.corba.se.spi.activation.ServerAlreadyUninstalled;
  43. import com.sun.corba.se.spi.activation.ServerNotRegistered;
  44. import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo;
  45. import com.sun.corba.se.spi.transport.SocketOrChannelAcceptor;
  46. import com.sun.corba.se.spi.orb.ORB;
  47. import com.sun.corba.se.impl.orbutil.ORBConstants;
  48. import com.sun.corba.se.spi.logging.CORBALogDomains;
  49. import com.sun.corba.se.impl.logging.ActivationSystemException;
  50. /**
  51. *
  52. * @version 1.1, 97/10/17
  53. * @author Rohit Garg
  54. * @since JDK1.2
  55. */
  56. public class RepositoryImpl extends _RepositoryImplBase
  57. implements Serializable
  58. {
  59. // added serialver computed by the tool
  60. private static final long serialVersionUID = 8458417785209341858L;
  61. RepositoryImpl(ORB orb, File dbDir, boolean debug)
  62. {
  63. this.debug = debug ;
  64. this.orb = orb;
  65. wrapper = ActivationSystemException.get( orb, CORBALogDomains.ORBD_REPOSITORY ) ;
  66. // if databse does not exist, create it otherwise read it in
  67. File dbFile = new File(dbDir, "servers.db");
  68. if (!dbFile.exists()) {
  69. db = new RepositoryDB(dbFile);
  70. db.flush();
  71. } else {
  72. try {
  73. FileInputStream fis = new FileInputStream(dbFile);
  74. ObjectInputStream ois = new ObjectInputStream(fis);
  75. db = (RepositoryDB) ois.readObject();
  76. ois.close();
  77. } catch (Exception e) {
  78. throw wrapper.cannotReadRepositoryDb( e ) ;
  79. }
  80. }
  81. // export the repository
  82. orb.connect(this);
  83. }
  84. private String printServerDef( ServerDef sd )
  85. {
  86. return "ServerDef[applicationName=" + sd.applicationName +
  87. " serverName=" + sd.serverName +
  88. " serverClassPath=" + sd.serverClassPath +
  89. " serverArgs=" + sd. serverArgs +
  90. " serverVmArgs=" + sd.serverVmArgs +
  91. "]" ;
  92. }
  93. public int registerServer(ServerDef serverDef, int theServerId)
  94. throws ServerAlreadyRegistered
  95. {
  96. int serverId;
  97. DBServerDef server = null;
  98. synchronized (db) {
  99. // check if server already registered
  100. Enumeration enumeration = db.serverTable.elements();
  101. while (enumeration.hasMoreElements()) {
  102. server = (DBServerDef) enumeration.nextElement();
  103. if (serverDef.applicationName.equals(server.applicationName)) {
  104. if (debug)
  105. System.out.println(
  106. "RepositoryImpl: registerServer called " +
  107. "to register ServerDef " +
  108. printServerDef( serverDef ) +
  109. " with " + ((theServerId==illegalServerId) ?
  110. "a new server Id" : ("server Id " + theServerId)) +
  111. " FAILED because it is already registered." ) ;
  112. throw (new ServerAlreadyRegistered(server.id));
  113. }
  114. }
  115. // generate a new server id
  116. if (theServerId == illegalServerId)
  117. serverId = db.incrementServerIdCounter();
  118. else
  119. serverId = theServerId;
  120. // add server def to the database
  121. server = new DBServerDef(serverDef, serverId);
  122. db.serverTable.put(new Integer(serverId), server);
  123. db.flush();
  124. if (debug)
  125. if (theServerId==illegalServerId)
  126. System.out.println( "RepositoryImpl: registerServer called " +
  127. "to register ServerDef " + printServerDef( serverDef ) +
  128. " with new serverId " + serverId ) ;
  129. else
  130. System.out.println( "RepositoryImpl: registerServer called " +
  131. "to register ServerDef " + printServerDef( serverDef ) +
  132. " with assigned serverId " + serverId ) ;
  133. return serverId;
  134. }
  135. }
  136. public int registerServer(ServerDef serverDef)
  137. throws ServerAlreadyRegistered, BadServerDefinition
  138. {
  139. // verify that the entry is valid
  140. LegacyServerSocketEndPointInfo endpoint =
  141. orb.getLegacyServerSocketManager()
  142. .legacyGetEndpoint(LegacyServerSocketEndPointInfo.BOOT_NAMING);
  143. int initSvcPort = ((SocketOrChannelAcceptor)endpoint)
  144. .getServerSocket().getLocalPort();
  145. ServerTableEntry entry = new ServerTableEntry( wrapper,
  146. illegalServerId, serverDef, (int) initSvcPort, "", true, debug );
  147. switch (entry.verify()) {
  148. case ServerMain.OK:
  149. break;
  150. case ServerMain.MAIN_CLASS_NOT_FOUND:
  151. throw new BadServerDefinition("main class not found.");
  152. case ServerMain.NO_MAIN_METHOD:
  153. throw new BadServerDefinition("no main method found.");
  154. case ServerMain.APPLICATION_ERROR:
  155. throw new BadServerDefinition("server application error.");
  156. default:
  157. throw new BadServerDefinition("unknown Exception.");
  158. }
  159. return registerServer(serverDef, illegalServerId);
  160. }
  161. public void unregisterServer(int serverId) throws ServerNotRegistered {
  162. DBServerDef server = null;
  163. Integer id = new Integer(serverId);
  164. synchronized (db) {
  165. // check to see if the server is registered
  166. server = (DBServerDef) db.serverTable.get(id);
  167. if (server == null) {
  168. if (debug)
  169. System.out.println(
  170. "RepositoryImpl: unregisterServer for serverId " +
  171. serverId + " called: server not registered" ) ;
  172. throw (new ServerNotRegistered());
  173. }
  174. // remove server from the database
  175. db.serverTable.remove(id);
  176. db.flush();
  177. }
  178. if (debug)
  179. System.out.println(
  180. "RepositoryImpl: unregisterServer for serverId " + serverId +
  181. " called" ) ;
  182. }
  183. private DBServerDef getDBServerDef(int serverId) throws ServerNotRegistered
  184. {
  185. Integer id = new Integer(serverId);
  186. DBServerDef server = (DBServerDef) db.serverTable.get(id);
  187. if (server == null)
  188. throw new ServerNotRegistered( serverId );
  189. return server ;
  190. }
  191. public ServerDef getServer(int serverId) throws ServerNotRegistered
  192. {
  193. DBServerDef server = getDBServerDef( serverId ) ;
  194. ServerDef serverDef = new ServerDef(server.applicationName, server.name,
  195. server.classPath, server.args, server.vmArgs);
  196. if (debug)
  197. System.out.println(
  198. "RepositoryImpl: getServer for serverId " + serverId +
  199. " returns " + printServerDef( serverDef ) ) ;
  200. return serverDef;
  201. }
  202. public boolean isInstalled(int serverId) throws ServerNotRegistered {
  203. DBServerDef server = getDBServerDef( serverId ) ;
  204. return server.isInstalled ;
  205. }
  206. public void install( int serverId )
  207. throws ServerNotRegistered, ServerAlreadyInstalled
  208. {
  209. DBServerDef server = getDBServerDef( serverId ) ;
  210. if (server.isInstalled)
  211. throw new ServerAlreadyInstalled( serverId ) ;
  212. else {
  213. server.isInstalled = true ;
  214. db.flush() ;
  215. }
  216. }
  217. public void uninstall( int serverId )
  218. throws ServerNotRegistered, ServerAlreadyUninstalled
  219. {
  220. DBServerDef server = getDBServerDef( serverId ) ;
  221. if (!server.isInstalled)
  222. throw new ServerAlreadyUninstalled( serverId ) ;
  223. else {
  224. server.isInstalled = false ;
  225. db.flush() ;
  226. }
  227. }
  228. public int[] listRegisteredServers() {
  229. synchronized (db) {
  230. int i=0;
  231. int servers[] = new int[db.serverTable.size()];
  232. Enumeration enumeration = db.serverTable.elements();
  233. while (enumeration.hasMoreElements()) {
  234. DBServerDef server = (DBServerDef) enumeration.nextElement();
  235. servers[i++] = server.id;
  236. }
  237. if (debug) {
  238. StringBuffer sb = new StringBuffer() ;
  239. for (int ctr=0; ctr<servers.length; ctr++) {
  240. sb.append( ' ' ) ;
  241. sb.append( servers[ctr] ) ;
  242. }
  243. System.out.println(
  244. "RepositoryImpl: listRegisteredServers returns" +
  245. sb.toString() ) ;
  246. }
  247. return servers;
  248. }
  249. }
  250. public int getServerID(String applicationName) throws ServerNotRegistered {
  251. synchronized (db) {
  252. int result = -1 ;
  253. for (Enumeration serverIds = db.serverTable.keys();
  254. serverIds.hasMoreElements();)
  255. {
  256. Integer nextServerId = (Integer) serverIds.nextElement();
  257. DBServerDef dbServerDef =
  258. (DBServerDef) db.serverTable.get(nextServerId);
  259. if (dbServerDef.applicationName.equals(applicationName)) {
  260. result = nextServerId.intValue();
  261. break ;
  262. }
  263. }
  264. if (debug)
  265. System.out.println("RepositoryImpl: getServerID for " +
  266. applicationName + " is " + result ) ;
  267. if (result == -1) {
  268. throw (new ServerNotRegistered());
  269. } else {
  270. return result ;
  271. }
  272. }
  273. }
  274. public String[] getApplicationNames() {
  275. synchronized (db) {
  276. Vector v = new Vector();
  277. for (Enumeration serverIds = db.serverTable.keys();
  278. serverIds.hasMoreElements();)
  279. {
  280. Integer nextServerId = (Integer) serverIds.nextElement();
  281. DBServerDef dbServerDef = (DBServerDef)db.serverTable.get(
  282. nextServerId);
  283. if (!dbServerDef.applicationName.equals(""))
  284. v.addElement( dbServerDef.applicationName ) ;
  285. }
  286. String[] apps = new String[v.size()];
  287. for (int i = 0; i < v.size(); i++) {
  288. apps[i] = (String)v.elementAt(i);
  289. }
  290. if (debug) {
  291. StringBuffer sb = new StringBuffer() ;
  292. for (int ctr=0; ctr<apps.length; ctr++) {
  293. sb.append( ' ' ) ;
  294. sb.append( apps[ctr] ) ;
  295. }
  296. System.out.println( "RepositoryImpl: getApplicationNames returns " +
  297. sb.toString() ) ;
  298. }
  299. return apps;
  300. }
  301. }
  302. /**
  303. * Typically the Repositoy is created within the ORBd VM but it can
  304. * be independently started as well.
  305. */
  306. public static void main(String args[]) {
  307. boolean debug = false ;
  308. for (int ctr=0; ctr<args.length; ctr++)
  309. if (args[ctr].equals("-debug"))
  310. debug = true ;
  311. try {
  312. // See Bug 4396928 for more information about why we are
  313. // initializing the ORBClass to PIORB (now ORBImpl, but see the bug).
  314. Properties props = new Properties();
  315. props.put("org.omg.CORBA.ORBClass",
  316. "com.sun.corba.se.impl.orb.ORBImpl");
  317. ORB orb = (ORB) ORB.init(args, props);
  318. // create the repository object
  319. String db = System.getProperty( ORBConstants.DB_PROPERTY,
  320. ORBConstants.DEFAULT_DB_NAME );
  321. RepositoryImpl repository = new RepositoryImpl(orb, new File(db),
  322. debug);
  323. // wait for shutdown
  324. orb.run();
  325. } catch (Exception ex) {
  326. ex.printStackTrace();
  327. }
  328. }
  329. transient private boolean debug = false;
  330. final static int illegalServerId = -1;
  331. transient private RepositoryDB db = null;
  332. transient ORB orb = null;
  333. transient ActivationSystemException wrapper ;
  334. class RepositoryDB implements Serializable
  335. {
  336. File db;
  337. Hashtable serverTable;
  338. Integer serverIdCounter;
  339. RepositoryDB(File dbFile) {
  340. db = dbFile;
  341. // initialize the Server Id counter and hashtable.
  342. // the lower id range is reserved for system servers
  343. serverTable = new Hashtable(255);
  344. serverIdCounter = new Integer(256);
  345. }
  346. int incrementServerIdCounter()
  347. {
  348. int value = serverIdCounter.intValue();
  349. serverIdCounter = new Integer(++value);
  350. return value;
  351. }
  352. void flush()
  353. {
  354. try {
  355. db.delete();
  356. FileOutputStream fos = new FileOutputStream(db);
  357. ObjectOutputStream oos = new ObjectOutputStream(fos);
  358. oos.writeObject(this);
  359. oos.flush();
  360. oos.close();
  361. } catch (Exception ex) {
  362. throw wrapper.cannotWriteRepositoryDb( ex ) ;
  363. }
  364. }
  365. }
  366. class DBServerDef implements Serializable
  367. {
  368. public String toString() {
  369. return "DBServerDef(applicationName=" + applicationName +
  370. ", name=" + name +
  371. ", classPath=" + classPath +
  372. ", args=" + args +
  373. ", vmArgs=" + vmArgs +
  374. ", id=" + id +
  375. ", isInstalled=" + isInstalled + ")" ;
  376. }
  377. DBServerDef(ServerDef server, int server_id) {
  378. applicationName = server.applicationName ;
  379. name = server.serverName;
  380. classPath = server.serverClassPath;
  381. args = server.serverArgs;
  382. vmArgs = server.serverVmArgs;
  383. id = server_id;
  384. isInstalled = false ;
  385. }
  386. String applicationName;
  387. String name;
  388. String classPath;
  389. String args;
  390. String vmArgs;
  391. boolean isInstalled ;
  392. int id;
  393. }
  394. }