001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClientTimer.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.examples.timerservice;
025:
026: import java.util.Hashtable;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031:
032: /**
033: * Client of the timer bean.
034: * @author Florent Benoit
035: *
036: */
037: public final class ClientTimer {
038:
039: /**
040: * Default InitialContextFactory to use.
041: */
042: private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
043:
044: /**
045: * Utility class.
046: */
047: private ClientTimer() {
048:
049: }
050:
051: /**
052: * Main method.
053: * @param args the arguments (not required)
054: * @throws Exception if exception is found.
055: */
056: public static void main(final String[] args) throws Exception {
057: Context initialContext = getInitialContext();
058:
059: TimerBeanRemote timerBean = (TimerBeanRemote) initialContext
060: .lookup("timerBean");
061:
062: System.out
063: .println("Calling init method that will fire a new timer...");
064: timerBean.init();
065: }
066:
067: /**
068: * @return Returns the InitialContext.
069: * @throws NamingException If the Context cannot be created.
070: */
071: private static Context getInitialContext() throws NamingException {
072:
073: // if user don't use jclient/client container
074: // we can specify the InitialContextFactory to use
075: // But this is *not recommended*.
076: Hashtable<String, Object> env = new Hashtable<String, Object>();
077: env.put(Context.INITIAL_CONTEXT_FACTORY,
078: getInitialContextFactory());
079:
080: // Usually a simple new InitialContext() without any parameters is sufficent.
081: // return new InitialContext();
082:
083: return new InitialContext(env);
084: }
085:
086: /**
087: * Returns a configurable InitialContextFactory classname.<br/>
088: * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
089: * @return Returns a configurable InitialContextFactory classname.
090: */
091: private static String getInitialContextFactory() {
092: String prop = System
093: .getProperty("easybeans.client.initial-context-factory");
094: // If not found, use the default
095: if (prop == null) {
096: prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
097: }
098: return prop;
099: }
100:
101: }
|