[Mc4j-cvs] mc4j/src/org/mc4j/console/dashboard/match AbstractBeanCondition.java,NONE,1.1 BeanConditi
Brought to you by:
ghinkl
From: Greg H. <gh...@us...> - 2004-04-07 03:05:29
|
Update of /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/match In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14931/src/org/mc4j/console/dashboard/match Added Files: AbstractBeanCondition.java BeanCondition.java BeanMatch.java BeanObjectNameCondition.java DashboardMatch.java MatchExecutor.java ServerAttributeCondition.java Log Message: This is an implementation of the new multiple matching system. It supports multiple bean/list matches per dashboard and multiple conditions per match. --- NEW FILE: BeanMatch.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard.match; import java.util.List; import java.util.ArrayList; /** * @author Greg Hinkle (gh...@us...), Apr 4, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/07 02:52:33 $) */ public class BeanMatch { public static final String TYPE_SINGLE = "Single"; public static final String TYPE_MULTIPLE = "Multiple"; private String id; private String type; private List conditionList = new ArrayList(); public String getId() { return id; } /** * @param id the name within the global dashboard context at which the * matching bean can be found. */ public void setId(String id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public List getConditionList() { return conditionList; } public void setConditionList(List conditionList) { this.conditionList = conditionList; } public void addCondition(BeanCondition beanCondition) { this.conditionList.add(beanCondition); } } --- NEW FILE: BeanCondition.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard.match; import org.mc4j.console.bean.MBeanNode; import org.w3c.dom.Element; /** * @author Greg Hinkle (gh...@us...), Apr 4, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/07 02:52:33 $) */ public interface BeanCondition { public void initializeCondition(Element rootElement); public boolean testCondition(MBeanNode node); } --- NEW FILE: AbstractBeanCondition.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard.match; import java.util.Set; import java.util.Iterator; import java.util.HashMap; import java.util.Map; import java.beans.Introspector; import java.beans.IntrospectionException; import java.beans.BeanInfo; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import org.openide.ErrorManager; import org.w3c.dom.Element; /** * @author Greg Hinkle (gh...@us...), Apr 5, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/07 02:52:33 $) */ public abstract class AbstractBeanCondition implements BeanCondition { public void initializeCondition(Element rootElement) { String[] conditionAttributes = getConditionAttributes(); try { BeanInfo conditionBeanInfo = Introspector.getBeanInfo(this.getClass()); PropertyDescriptor[] descriptors = conditionBeanInfo.getPropertyDescriptors(); Map propertyMap = new HashMap(); for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor descriptor = descriptors[i]; propertyMap.put(descriptor.getName(),descriptor); } for (int i = 0; i < conditionAttributes.length; i++) { String attribute = conditionAttributes[i]; PropertyDescriptor property = (PropertyDescriptor) propertyMap.get(attribute); if (property != null) { String attributeValue = rootElement.getAttribute(attribute); if (attributeValue != null) { Method writeMethod = property.getWriteMethod(); writeMethod.invoke(this, new Object[] { attributeValue }); } } } } catch(IntrospectionException ie) { ErrorManager.getDefault().notify(ie); } catch(IllegalAccessException iae) { ErrorManager.getDefault().notify(iae); } catch(IllegalArgumentException iae) { ErrorManager.getDefault().notify(iae); } catch(InvocationTargetException ite) { ErrorManager.getDefault().notify(ite); } } /** * * @return the set of attribute names that are a part of this condition and * should be read from the xml into the implementing condition object. */ public abstract String[] getConditionAttributes(); } --- NEW FILE: DashboardMatch.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard.match; import java.util.List; import java.util.ArrayList; /** * @author Greg Hinkle (gh...@us...), Apr 4, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/07 02:52:33 $) */ public class DashboardMatch { private String type; private String location; public static final String TYPE_GLOBAL = "Global"; public static final String TYPE_BEAN = "Bean"; private List beanMatchList = new ArrayList(); public String getType() { return type; } public void setType(String type) { if (TYPE_GLOBAL.equals(type) || TYPE_BEAN.equals(type)) { this.type = type; } else { throw new IllegalArgumentException( "DashboardMatch [type] attribute must be one of {Global, Bean}."); } } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public List getBeanMatchList() { return beanMatchList; } public void setBeanMatchList(List beanMatchList) { this.beanMatchList = beanMatchList; } public void addBeanMatch(BeanMatch beanMatch) { this.beanMatchList.add(beanMatch); } } --- NEW FILE: ServerAttributeCondition.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard.match; import org.mc4j.console.bean.MBeanNode; import org.w3c.dom.Element; /** * @author Greg Hinkle (gh...@us...), Apr 5, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/07 02:52:33 $) */ public class ServerAttributeCondition extends AbstractBeanCondition { private String serverType; public String[] getConditionAttributes() { return new String[] { "serverType" }; } public String getServerType() { return serverType; } public void setServerType(String serverType) { this.serverType = serverType; } public boolean testCondition(MBeanNode node) { return serverType.equals(node.getConnectionNode().GetConnectionType()); } } --- NEW FILE: MatchExecutor.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard.match; import java.util.List; import java.util.Iterator; import java.util.ArrayList; import org.mc4j.console.dashboard.Dashboard; import org.mc4j.console.bean.MBeanNode; import org.mc4j.console.connection.ConnectionNode; /** * @author Greg Hinkle (gh...@us...), Apr 5, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/07 02:52:33 $) */ public class MatchExecutor { private static MatchExecutor instance; public static MatchExecutor getInstance() { if (instance == null) { instance = new MatchExecutor(); } return instance; } private MatchExecutor() { } public boolean matchSingle(Dashboard dashboard, MBeanNode mBeanNode) { DashboardMatch dashboardMatch = dashboard.getDashboardMatch(); if (DashboardMatch.TYPE_BEAN.equals(dashboardMatch.getType())) { List beanMatchList = dashboardMatch.getBeanMatchList(); // We only allow one bean match for "Bean" type dashboard matches BeanMatch beanMatch = (BeanMatch) beanMatchList.get(0); List conditionList = beanMatch.getConditionList(); if (beanConditionListMatch(conditionList, mBeanNode)) { dashboard.putContext(beanMatch.getId(), mBeanNode); return true; } } return false; } /** * * @param dashboard * @param connectionNode * @return null if no matches are found, otherwise a list of dashboards with the * associated bindings. */ public List matchGlobalDashboards(Dashboard dashboard, ConnectionNode connectionNode) { List matches = new ArrayList(); DashboardMatch dashboardMatch = dashboard.getDashboardMatch(); List mbeanNodes = connectionNode.getMBeanList(); boolean allBeansMatched = true; if (DashboardMatch.TYPE_GLOBAL.equals(dashboardMatch.getType())) { List beanMatchList = dashboardMatch.getBeanMatchList(); for (Iterator iterator = beanMatchList.iterator(); iterator.hasNext();) { BeanMatch beanMatch = (BeanMatch) iterator.next(); List conditionList = beanMatch.getConditionList(); boolean beanMatched = false; if (BeanMatch.TYPE_MULTIPLE.equals(beanMatch.getType())) { List matchedBeanList = new ArrayList(); for (Iterator iterator1 = mbeanNodes.iterator(); iterator1.hasNext();) { MBeanNode mBeanNode = (MBeanNode) iterator1.next(); if (beanConditionListMatch(conditionList, mBeanNode)) { matchedBeanList.add(mBeanNode); beanMatched = true; } } dashboard.putContext(beanMatch.getId(), matchedBeanList); } else if (BeanMatch.TYPE_SINGLE.equals(beanMatch.getType())) { for (Iterator iterator1 = mbeanNodes.iterator(); iterator1.hasNext();) { MBeanNode mBeanNode = (MBeanNode) iterator1.next(); if (beanConditionListMatch(conditionList, mBeanNode)) { dashboard.putContext(beanMatch.getId(), mBeanNode); beanMatched = true; } } } if (!beanMatched) return null; } matches.add(dashboard); } if (matches.isEmpty()) { return null; } else { return matches; } } /** * Test all conditions. * @param conditionList * @param mBeanNode * @return */ protected boolean beanConditionListMatch(List conditionList, MBeanNode mBeanNode) { boolean match = true; for (Iterator iterator = conditionList.iterator(); iterator.hasNext();) { BeanCondition beanCondition = (BeanCondition) iterator.next(); if (!beanCondition.testCondition(mBeanNode)) match = false; } return match; } } --- NEW FILE: BeanObjectNameCondition.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard.match; import org.openide.ErrorManager; import org.openide.windows.IOProvider; import org.mc4j.console.bean.MBeanNode; import org.apache.regexp.RE; import org.apache.regexp.RESyntaxException; /** * @author Greg Hinkle (gh...@us...), Apr 5, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/07 02:52:33 $) */ public class BeanObjectNameCondition extends AbstractBeanCondition { private String filter; private RE nameFilter; public String[] getConditionAttributes() { return new String[] { "filter" }; } public String getFilter() { return filter; } public void setFilter(String filter) { this.filter = filter; try { this.nameFilter = new RE(filter); //REUtil.createRE(filter); } catch (RESyntaxException rese) { ErrorManager.getDefault().notify(rese); IOProvider.getDefault().getStdOut().println( "Invalid dashboard matching regular expression [" + filter + "] in dashboard."); } } public boolean testCondition(MBeanNode node) { String cName = node.getObjectName().getCanonicalName(); if (this.nameFilter != null) { return this.nameFilter.match(cName); } else { return true; } } } |