001: package com.sun.portal.fabric.tasks;
002:
003: import weblogic.jndi.Environment;
004: import weblogic.management.MBeanHome;
005: import weblogic.management.configuration.ServerStartMBean;
006: import weblogic.management.configuration.ServerMBean;
007: import weblogic.management.configuration.JDBCTxDataSourceMBean;
008: import weblogic.management.configuration.JDBCConnectionPoolMBean;
009:
010: import com.sun.portal.log.common.PortalLogger;
011:
012: import javax.naming.Context;
013: import java.util.Set;
014: import java.util.Iterator;
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017: import java.util.StringTokenizer;
018:
019: public class WeblogicServerHelper {
020: private static Logger logger = PortalLogger
021: .getLogger(WeblogicServerHelper.class);
022:
023: public static void main(String[] args) throws Exception {
024: System.out.println("Indise WeblogicServerHelper main\n"
025: + args.length);
026: if (args.length < 2 || !args[0].equalsIgnoreCase("-method")) {
027: throw new Exception(
028: "Usuage: WeblogicServerHelper -method <methodname> [-params < > < > ...] ");
029: }
030:
031: //args[1] is the method name.
032: if (args[1].equals("setManagedServerClasspath")) {
033: if (args.length < 5)
034: throw new Exception(
035: "Usuage: WeblogicServerHelper -method setManagedServerClasspath -params adminurl username password instancename classpath ");
036: setManagedServerClasspath(args[3], args[4], args[5],
037: args[6], args[7]);
038: }
039:
040: if (args[1].equals("setManagedServerArguments")) {
041: if (args.length < 5)
042: throw new Exception(
043: "Usuage: WeblogicServerHelper -method setManagedServerArguments -params adminurl username password instancename args ");
044: setManagedServerArguments(args[3], args[4], args[5],
045: args[6], args[7]);
046: }
047:
048: if (args[1].equals("setTargetsToDatasources")) {
049: setTargetsToDatasources(args[3], args[4], args[5], args[6],
050: args[7]);
051: }
052:
053: if (args[1].equals("setTargetsToConnectionPools")) {
054: setTargetsToConnectionPools(args[3], args[4], args[5],
055: args[6]);
056: }
057:
058: if (args[1].equals("removeManagedServerArguments")) {
059: removeManagedServerArguments(args[3], args[4], args[5],
060: args[6]);
061: }
062:
063: if (args[1].equals("removeManagedServerClasspath")) {
064: removeManagedServerClasspath(args[3], args[4], args[5],
065: args[6]);
066: }
067:
068: if (args[1].equals("removeTargetsToDatasources")) {
069: removeTargetsToDatasources(args[3], args[4], args[5],
070: args[6], args[7]);
071: }
072:
073: if (args[1].equals("removeTargetsToConnectionPools")) {
074: removeTargetsToConnectionPools(args[3], args[4], args[5],
075: args[6]);
076: }
077:
078: }
079:
080: private static void setManagedServerClasspath(String url,
081: String username, String password, String wcTargetInstance,
082: String classpath) throws Exception {
083: // System.out.println("Inside setManagedServerClasspath");
084: try {
085: //Obtaining an MBeanHome Using JNDI
086: //This is bean is used to obtain references to all other weblogic mbeans.
087: Environment env = new Environment();
088: env.setProviderUrl(url);
089: env.setSecurityPrincipal(username);
090: env.setSecurityCredentials(password);
091: Context ctx = env.getInitialContext();
092: MBeanHome home = (MBeanHome) ctx
093: .lookup(MBeanHome.ADMIN_JNDI_NAME);
094:
095: Set allServerStartMBeans = home
096: .getMBeansByType("ServerStart");
097:
098: for (Iterator itr = allServerStartMBeans.iterator(); itr
099: .hasNext();) {
100: ServerStartMBean mbean = (ServerStartMBean) itr.next();
101: String oldClasspath = mbean.getClassPath();
102: if (mbean.getName().equals(wcTargetInstance)) {
103: //if there is an classpath set, and it does not contain portal jars already
104: if (oldClasspath != null
105: && oldClasspath
106: .indexOf(WebContainerBase.PORTAL_TEST_JAR) < 0)
107: classpath = classpath + ":" + oldClasspath;
108: mbean.setClassPath(classpath);
109: }
110: }
111: } catch (Exception e) {
112: logger.log(Level.SEVERE, "PSFB_CSPFT0328", e);
113: throw new Exception(
114: "Unable to set Managed server start classpath: "
115: + e.toString());
116: }
117: }
118:
119: private static void setManagedServerArguments(String url,
120: String username, String password, String wcTargetInstance,
121: String args) throws Exception {
122: try {
123: //Obtaining an MBeanHome Using JNDI
124: //This is bean is used to obtain references to all other weblogic mbeans.
125: String newargs = null;
126: StringTokenizer st = new StringTokenizer(args, "\"");
127: newargs = st.nextToken();
128: if (newargs == null || newargs.equals("")) {
129: newargs = args;
130: }
131: Environment env = new Environment();
132: env.setProviderUrl(url);
133: env.setSecurityPrincipal(username);
134: env.setSecurityCredentials(password);
135: Context ctx = env.getInitialContext();
136: MBeanHome home = (MBeanHome) ctx
137: .lookup(MBeanHome.ADMIN_JNDI_NAME);
138:
139: Set allServerStartMBeans = home
140: .getMBeansByType("ServerStart");
141: for (Iterator itr = allServerStartMBeans.iterator(); itr
142: .hasNext();) {
143: ServerStartMBean mbean = (ServerStartMBean) itr.next();
144: String oldArgs = mbean.getArguments();
145: if (mbean.getName().equals(wcTargetInstance)) {
146: //if there are no existing args, or if these args set are not already set
147: if (oldArgs != null && oldArgs.indexOf(args) < 0)
148: newargs = newargs + " " + oldArgs;
149: }
150: mbean.setArguments(newargs);
151: }
152: } catch (Exception e) {
153: logger.log(Level.SEVERE, "PSFB_CSPFT0328", e);
154: throw new Exception(
155: "Unable to set Managed server start classpath: "
156: + e.toString());
157: }
158: }
159:
160: private static void setTargetsToDatasources(String url,
161: String username, String password, String wcTargetInstance,
162: String jndiName) throws Exception {
163:
164: try {
165: //Obtaining an MBeanHome Using JNDI
166: //This is bean is used to obtain references to all other weblogic mbeans.
167: Environment env = new Environment();
168: env.setProviderUrl(url);
169: env.setSecurityPrincipal(username);
170: env.setSecurityCredentials(password);
171: Context ctx = env.getInitialContext();
172: MBeanHome home = (MBeanHome) ctx
173: .lookup(MBeanHome.ADMIN_JNDI_NAME);
174:
175: Set allServerMBeans = home.getMBeansByType("Server");
176: ServerMBean serverMbean = null;
177: for (Iterator itr = allServerMBeans.iterator(); itr
178: .hasNext();) {
179: serverMbean = (ServerMBean) itr.next();
180: if (serverMbean.getName().equals(wcTargetInstance)) {
181: break;
182: }
183: }
184:
185: Set allDataSourceMBeans = home
186: .getMBeansByType("JDBCTxDataSource");
187: for (Iterator itr = allDataSourceMBeans.iterator(); itr
188: .hasNext();) {
189: JDBCTxDataSourceMBean mbean = (JDBCTxDataSourceMBean) itr
190: .next();
191: if (mbean.getJNDIName().equals(jndiName)) {
192: mbean.addTarget(serverMbean);
193: }
194: }
195: } catch (Exception e) {
196: logger.log(Level.SEVERE, "PSFB_CSPFT0329", e);
197: throw new Exception(
198: "Unable to add targets to the datasources: "
199: + e.toString());
200: }
201: }
202:
203: private static void setTargetsToConnectionPools(String url,
204: String username, String password, String wcTargetInstance)
205: throws Exception {
206:
207: try {
208: //Obtaining an MBeanHome Using JNDI
209: //This is bean is used to obtain references to all other weblogic mbeans.
210: Environment env = new Environment();
211: env.setProviderUrl(url);
212: env.setSecurityPrincipal(username);
213: env.setSecurityCredentials(password);
214: Context ctx = env.getInitialContext();
215: MBeanHome home = (MBeanHome) ctx
216: .lookup(MBeanHome.ADMIN_JNDI_NAME);
217:
218: Set allServerMBeans = home.getMBeansByType("Server");
219: ServerMBean serverMbean = null;
220: for (Iterator itr = allServerMBeans.iterator(); itr
221: .hasNext();) {
222: serverMbean = (ServerMBean) itr.next();
223: if (serverMbean.getName().equals(wcTargetInstance)) {
224: break;
225: }
226: }
227:
228: Set allConnectionPoolMBeans = home
229: .getMBeansByType("JDBCConnectionPool");
230: for (Iterator itr = allConnectionPoolMBeans.iterator(); itr
231: .hasNext();) {
232: JDBCConnectionPoolMBean mbean = (JDBCConnectionPoolMBean) itr
233: .next();
234: mbean.addTarget(serverMbean);
235: }
236: } catch (Exception e) {
237: logger.log(Level.SEVERE, "PSFB_CSPFT0330", e);
238: throw new Exception(
239: "Unable to add targets to the connection pool: "
240: + e.toString());
241: }
242: }
243:
244: private static void removeManagedServerClasspath(String url,
245: String username, String password, String wcTargetInstance)
246: throws Exception {
247: try {
248: //Obtaining an MBeanHome Using JNDI
249: //This is bean is used to obtain references to all other weblogic mbeans.
250: Environment env = new Environment();
251: env.setProviderUrl(url);
252: env.setSecurityPrincipal(username);
253: env.setSecurityCredentials(password);
254: Context ctx = env.getInitialContext();
255: MBeanHome home = (MBeanHome) ctx
256: .lookup(MBeanHome.ADMIN_JNDI_NAME);
257:
258: Set allServerStartMBeans = home
259: .getMBeansByType("ServerStart");
260:
261: for (Iterator itr = allServerStartMBeans.iterator(); itr
262: .hasNext();) {
263: ServerStartMBean mbean = (ServerStartMBean) itr.next();
264: if (mbean.getName().equals(wcTargetInstance)) {
265: mbean.setClassPath("");
266: }
267: }
268:
269: } catch (Exception e) {
270: logger.log(Level.SEVERE, "PSFB_CSPFT0328", e);
271: throw new Exception(
272: "Unable to remove Managed server start classpath: "
273: + e.toString());
274: }
275: }
276:
277: private static void removeManagedServerArguments(String url,
278: String username, String password, String wcTargetInstance)
279: throws Exception {
280: try {
281: //Obtaining an MBeanHome Using JNDI
282: //This is bean is used to obtain references to all other weblogic mbeans.
283: Environment env = new Environment();
284: env.setProviderUrl(url);
285: env.setSecurityPrincipal(username);
286: env.setSecurityCredentials(password);
287: Context ctx = env.getInitialContext();
288: MBeanHome home = (MBeanHome) ctx
289: .lookup(MBeanHome.ADMIN_JNDI_NAME);
290:
291: Set allServerStartMBeans = home
292: .getMBeansByType("ServerStart");
293: for (Iterator itr = allServerStartMBeans.iterator(); itr
294: .hasNext();) {
295: ServerStartMBean mbean = (ServerStartMBean) itr.next();
296: if (mbean.getName().equals(wcTargetInstance)) {
297: logger
298: .log(
299: Level.INFO,
300: "Removing weblogic server arguments for instance",
301: wcTargetInstance);
302: mbean.setArguments("");
303:
304: }
305: }
306: } catch (Exception e) {
307: logger.log(Level.SEVERE, "PSFB_CSPFT0328", e);
308: throw new Exception(
309: "Unable to remove Managed server start arguments: "
310: + e.toString());
311: }
312: }
313:
314: private static void removeTargetsToDatasources(String url,
315: String username, String password, String wcTargetInstance,
316: String jndiName) throws Exception {
317:
318: try {
319: //Obtaining an MBeanHome Using JNDI
320: //This is bean is used to obtain references to all other weblogic mbeans.
321: Environment env = new Environment();
322: env.setProviderUrl(url);
323: env.setSecurityPrincipal(username);
324: env.setSecurityCredentials(password);
325: Context ctx = env.getInitialContext();
326: MBeanHome home = (MBeanHome) ctx
327: .lookup(MBeanHome.ADMIN_JNDI_NAME);
328:
329: Set allServerMBeans = home.getMBeansByType("Server");
330: ServerMBean serverMbean = null;
331: for (Iterator itr = allServerMBeans.iterator(); itr
332: .hasNext();) {
333: serverMbean = (ServerMBean) itr.next();
334: if (serverMbean.getName().equals(wcTargetInstance)) {
335: break;
336: }
337: }
338:
339: Set allDataSourceMBeans = home
340: .getMBeansByType("JDBCTxDataSource");
341: for (Iterator itr = allDataSourceMBeans.iterator(); itr
342: .hasNext();) {
343: JDBCTxDataSourceMBean mbean = (JDBCTxDataSourceMBean) itr
344: .next();
345: if (mbean.getJNDIName().equals(jndiName)) {
346: mbean.removeTarget(serverMbean);
347: }
348: }
349: } catch (Exception e) {
350: logger.log(Level.SEVERE, "PSFB_CSPFT0329", e);
351: throw new Exception(
352: "Unable to remove targets to the datasources: "
353: + e.toString());
354: }
355: }
356:
357: private static void removeTargetsToConnectionPools(String url,
358: String username, String password, String wcTargetInstance)
359: throws Exception {
360:
361: try {
362: //Obtaining an MBeanHome Using JNDI
363: //This is bean is used to obtain references to all other weblogic mbeans.
364: Environment env = new Environment();
365: env.setProviderUrl(url);
366: env.setSecurityPrincipal(username);
367: env.setSecurityCredentials(password);
368: Context ctx = env.getInitialContext();
369: MBeanHome home = (MBeanHome) ctx
370: .lookup(MBeanHome.ADMIN_JNDI_NAME);
371:
372: Set allServerMBeans = home.getMBeansByType("Server");
373: ServerMBean serverMbean = null;
374: for (Iterator itr = allServerMBeans.iterator(); itr
375: .hasNext();) {
376: serverMbean = (ServerMBean) itr.next();
377: if (serverMbean.getName().equals(wcTargetInstance)) {
378: break;
379: }
380: }
381:
382: Set allConnectionPoolMBeans = home
383: .getMBeansByType("JDBCConnectionPool");
384: for (Iterator itr = allConnectionPoolMBeans.iterator(); itr
385: .hasNext();) {
386: JDBCConnectionPoolMBean mbean = (JDBCConnectionPoolMBean) itr
387: .next();
388: mbean.removeTarget(serverMbean);
389: }
390: } catch (Exception e) {
391: logger.log(Level.SEVERE, "PSFB_CSPFT0330", e);
392: throw new Exception(
393: "Unable to remove targets to the connection pool: "
394: + e.toString());
395: }
396: }
397: }
|