[Mc4j-cvs] mc4j/src/org/mc4j/console/dashboard/context OgnlHelper.java,1.1,1.2
Brought to you by:
ghinkl
From: Greg H. <gh...@us...> - 2006-04-12 19:14:08
|
Update of /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/context In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20162/src/org/mc4j/console/dashboard/context Added Files: OgnlHelper.java Log Message: Merging EMS into head for the 2.0 release work --- NEW FILE: OgnlHelper.java --- /* * Copyright 2002-2005 Greg Hinkle * * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mc4j.console.dashboard.context; import ognl.ClassResolver; import ognl.DefaultClassResolver; import ognl.DefaultTypeConverter; import ognl.Ognl; import ognl.OgnlException; import ognl.OgnlRuntime; import ognl.PropertyAccessor; import ognl.TypeConverter; import org.mc4j.ems.connection.bean.EmsBean; import org.mc4j.ems.connection.bean.attribute.EmsAttribute; import org.openide.windows.IOProvider; import javax.management.openmbean.CompositeData; import javax.management.openmbean.TabularData; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.util.Map; import java.util.HashMap; import java.util.Date; /** * @author Greg Hinkle (gh...@us...), Apr 6, 2006 * @version $Revision: 1.2 $($Author: ghinkl $ / $Date: 2006/04/12 19:14:00 $) */ public class OgnlHelper { static TypeConverter converter = new ExtendedConvertor(); static ClassResolver resolver = new DefaultClassResolver(); static Formatter formatter = new Formatter(); public static Object getValue(String identifier, Map context, Class type) throws OgnlException { return getValue(identifier, context, context, type); } public static Object getValue(String identifier, Map context, Object root, Class type) throws OgnlException { if (context == null) { context = new HashMap(); } Ognl.setTypeConverter(context, converter); Ognl.setClassResolver(context, resolver); context.put("format",formatter); Object expression = Ognl.parseExpression(identifier); Object result = Ognl.getValue(expression, context, context, type); if (result == null) { // Looks like Ognl gets confused by classes loaded in other classloaders // Try again without asking for conversion result = Ognl.getValue(expression,context, root); } return result; } public static class Formatter { public String date(Number millis) { if (millis == null) return null; return (new Date(millis.longValue())).toString(); } private long[] millisTimes = { 1000L,60L,60L,24L,365L}; private String[] millisName = { "Second", "Minute", "Hour", "Day", "Year" }; public String timeMillis(Number time) { return time(time, millisTimes, millisName); } private long[] nanosTimes = { 1000000000L,60L,60L,24L,365L}; private String[] nanosName = {"Second", "Minute", "Hour", "Day", "Year" }; public String timeNanos(Number time) { return time(time, nanosTimes, nanosName); } private String time(Number time ,long[] spaces, String[] names) { long n = time.longValue(); long[] values = new long[spaces.length]; // Temporarily store the divisor in the values values[0] = spaces[0]; for (int i = 1;i<values.length;i++) values[i] = spaces[i] * values[i-1]; for (int i = values.length-1; i >=0; i--) { n -= values[i] * (values[i] = n / values[i]); } StringBuilder b = new StringBuilder(); for (int i = values.length-1; i >= 0; i--) { if (values[i] > 0) { if (b.length() > 0) b.append(" "); b.append(values[i]); b.append(" "); b.append(names[i]); if (values[i] > 1) b.append("s"); } } return b.toString(); } public String sizeBytes(Number bytes) { long l = (long) Math.log10(bytes.doubleValue()); if (l <3) { return String.format("%,.2f Bytes", bytes.doubleValue()); } else if (l < 6) { return String.format("%,.2f KB", bytes.doubleValue() / Math.pow(10d, 3d)); } else if (l < 9) { return String.format("%,.2f MB", bytes.doubleValue() / Math.pow(10d, 6d)); } else { return String.format("%,.2f GB", bytes.doubleValue() / Math.pow(10d, 9d)); } } } public static class ExtendedConvertor extends DefaultTypeConverter { public Object convertValue(Map context, Object value, Class toType) { Object result = null; if ((toType == Dimension.class) && (value instanceof String)) { String[] parts = ((String) value).split(","); result = new Dimension(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])); } else if (toType == Color.class && (value instanceof String)) { result = Color.decode((String) value); } else if (toType == Font.class && (value instanceof String)) { result = Font.decode((String) value); } else { result = super.convertValue(context, value, toType); } return result; } } static { OgnlRuntime.setPropertyAccessor(EmsBean.class, new EmsBeansPropertyAccessor()); OgnlRuntime.setPropertyAccessor(CompositeData.class, new CompositePropertyAccessor()); OgnlRuntime.setPropertyAccessor(TabularData.class, new TabularDataPropertyAccessor()); } public static class EmsBeansPropertyAccessor implements PropertyAccessor { public Object getProperty(Map context, Object target, Object name) throws OgnlException { EmsBean bean = (EmsBean) target; EmsAttribute attribute = bean.getAttribute((String) name); if (attribute == null) { IOProvider.getDefault().getIO("Dashboard debugging", false).getOut(). println("Attribute [" + name + "] not found on bean [" + bean.getBeanName() + "]"); return "?"; } return attribute.getValue(); } public void setProperty(Map map, Object object, Object object1, Object object2) throws OgnlException { throw new UnsupportedOperationException("Don't set attribute this way."); } } public static class CompositePropertyAccessor implements PropertyAccessor { public Object getProperty(Map context, Object target, Object name) throws OgnlException { CompositeData d = (CompositeData) target; return d.get((String) name); } public void setProperty(Map map, Object object, Object object1, Object object2) throws OgnlException { throw new UnsupportedOperationException("Don't set attributes this way."); } } public static class TabularDataPropertyAccessor implements PropertyAccessor { public Object getProperty(Map context, Object target, Object name) throws OgnlException { TabularData d = (TabularData) target; return d.get(new Object[]{name}); } public void setProperty(Map map, Object object, Object object1, Object object2) throws OgnlException { } } /* public static void main(String[] args) { Map context = new HashMap(); context.put("foo", new Date()); //Object result = OgnlHelper.getValue("'500,300'", context, Dimension.class); Object result = OgnlHelper.getValue("new javax.swing.JPanel()", context, null); System.out.println("Type: " + result.getClass().getName()); System.out.println("Value: " + result); } */ } |