001 package examples.ejb.ejb20.basic.beanManaged;
002
003 import java.rmi.RemoteException;
004 import java.util.Collection;
005 import java.util.Hashtable;
006 import java.util.Iterator;
007 import javax.ejb.CreateException;
008 import javax.ejb.FinderException;
009 import javax.ejb.RemoveException;
010 import javax.naming.Context;
011 import javax.naming.InitialContext;
012 import javax.naming.NamingException;
013 import javax.rmi.PortableRemoteObject;
014
015 /**
016 * <p>This class demonstrates calling an entity EJBean.
017 * This class also illustrates how to lookup an EJB home in the JNDI tree.
018 * </p>
019 *
020 * @author Copyright (c) 1998,2009, Oracle and/or its affiliates. All Rights Reserved.
021 */
022 public class Client {
023 private String url;
024 private AccountHome home;
025
026 public Client(String url) throws NamingException {
027 this.url = url;
028 home = lookupHome();
029 }
030
031 /**
032 * Runs this example from the command line. Example:
033 * <p>
034 * <tt>java examples.ejb.ejb20.basic.beanManaged.Client "t3://localhost:7001"</tt>
035 * <p>
036 * The parameters are optional, but if any are supplied,
037 * they are interpreted in this order:
038 * <p>
039 * @param args URL such as "t3://localhost:7001" of Server
040 */
041 public static void main(String[] args) throws Exception {
042 log("\nBeginning beanManaged.Client...\n");
043 Client client = null;
044 String url= "t3://localhost:7001";
045
046 // Parse the argument list
047 if (args.length > 1) {
048 log("Usage: java examples.ejb.ejb20.basic.beanManaged.Client t3://hostname:port");
049 return;
050 } else if (args.length == 1) {
051 url = args[0];
052 }
053
054 try {
055 client = new Client(url);
056 client.example();
057 } catch (NamingException ne) {
058 log("Unable to look up the beans home: " + ne.getMessage());
059 throw ne;
060 } catch (Exception e) {
061 log("There was an exception while creating and using the Accounts.");
062 log("This indicates that there was a problem communicating with the server: "+e);
063 throw e;
064 }
065 log("\nEnd beanManaged.Client...\n");
066 }
067
068 /**
069 *
070 * @throws CreateException
071 * @throws RemoteException
072 * @throws FinderException
073 * @throws RemoveException
074 */
075 public void example()
076 throws CreateException, RemoteException, FinderException, RemoveException
077 {
078 int numBeans = 20;
079 Account [] accounts = new Account [numBeans];
080
081 // Create 20 accounts
082 for (int i=0; i<numBeans; i++) {
083 accounts [i] = findOrCreateAccount("ID: "+i, i * 1000);
084 }
085
086 // print out the account balances
087 for (int i=0; i<numBeans; i++) {
088 log("Account: :"+accounts[i].getPrimaryKey()+
089 " has a balance of "+accounts[i].balance());
090 }
091
092 // find all accounts with a balance > 5000
093 findBigAccounts(5000.0);
094
095 // Remove our accounts
096 log("Removing beans...");
097 for (int i=0; i<numBeans; i++) {
098 accounts[i].remove();
099 }
100 }
101
102 /**
103 * List all accounts with a balance greater than the parameterized amount.
104 * This finder illustrates a Finder Method that returns a Collection.
105 */
106 private void findBigAccounts(double balanceGreaterThan)
107 throws FinderException, RemoteException {
108 log("\nQuerying for accounts with a balance greater than " +
109 balanceGreaterThan + "...");
110
111 Collection col = home.findBigAccounts(balanceGreaterThan);
112 if(col.isEmpty()) {
113 log("No accounts were found with a balance greater that "+
114 balanceGreaterThan);
115 }
116
117 Iterator it = col.iterator();
118 while (it.hasNext()) {
119 Account accountGT =
120 (Account) PortableRemoteObject.narrow(it.next(),Account.class);
121 log("Account " + accountGT.getPrimaryKey() +
122 "; balance is $" + accountGT.balance());
123 }
124 }
125
126 /**
127 * If there already exists an account for this id, we will return
128 * it. Otherwise we will create a new account.
129 */
130 private Account findOrCreateAccount(String id, double balance)
131 throws CreateException, FinderException, RemoteException {
132 try {
133 log("Creating account with id: "+id);
134 // create account with account id and balance.
135 return (Account) PortableRemoteObject.narrow(home.create(id, balance),
136 Account.class);
137 } catch (CreateException cex) {
138 // account id already exists.
139 return (Account) PortableRemoteObject.narrow(home.findByPrimaryKey(id),
140 Account.class);
141 }
142 }
143
144 /**
145 * Create a new account with the given id and balance
146 */
147 private Account createAccount(String id, double balance)
148 throws CreateException, RemoteException
149 {
150 log("Creating account " + id + " with a balance of " +
151 balance + "...");
152 Account ac = (Account) PortableRemoteObject.narrow(home.create(id, balance),
153 Account.class);
154 log("Account " + id + " successfully created");
155 return ac;
156 }
157
158 /**
159 * Look up the bean's home interface using JNDI.
160 */
161 private AccountHome lookupHome() throws NamingException {
162 Context ctx = getInitialContext();
163 try {
164 Object home = ctx.lookup("ejb20-beanManaged-AccountHome");
165 return (AccountHome) PortableRemoteObject.narrow(home, AccountHome.class);
166
167 } catch (NamingException ne) {
168 log("The client was unable to lookup the EJBHome. Please make sure " +
169 "that you have deployed the ejb with the JNDI name " +
170 "ejb20-beanManaged-AccountHome on the WebLogic server at "+url);
171 throw ne;
172 }
173 }
174
175 /**
176 * Get an initial context into the JNDI tree.
177 */
178 private Context getInitialContext() throws NamingException {
179 try {
180 // Get an InitialContext
181 Hashtable<String,String> h = new Hashtable<String,String>();
182 h.put(Context.INITIAL_CONTEXT_FACTORY,
183 "weblogic.jndi.WLInitialContextFactory");
184 h.put(Context.PROVIDER_URL, url);
185 return new InitialContext(h);
186 } catch (NamingException ne) {
187 log("We were unable to get a connection to the WebLogic server at "+url);
188 log("Please make sure that the server is running.");
189 throw ne;
190 }
191 }
192
193 private static void log(String s) { System.out.println(s); }
194 }
|