|
Glassfish example source code file (ConfigToolsImpl.java)
This example Glassfish source code file (ConfigToolsImpl.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Glassfish ConfigToolsImpl.java source code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.admin.amx.impl.config;
import com.sun.enterprise.config.serverbeans.Resource;
import com.sun.enterprise.config.serverbeans.SystemProperty;
import com.sun.enterprise.config.serverbeans.SystemPropertyBag;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import javax.management.ObjectName;
import org.glassfish.admin.amx.core.Util;
import org.glassfish.admin.amx.impl.mbean.AMXImplBase;
import org.glassfish.admin.amx.intf.config.ConfigTools;
import org.glassfish.api.admin.config.Named;
import org.jvnet.hk2.config.types.Property;
import org.jvnet.hk2.config.types.PropertyBag;
import org.jvnet.hk2.config.ConfigBean;
import org.jvnet.hk2.config.ConfigBeanProxy;
import org.jvnet.hk2.config.ConfigCode;
import org.jvnet.hk2.config.ConfigSupport;
import org.jvnet.hk2.config.TransactionFailure;
import org.jvnet.hk2.config.WriteableView;
import org.jvnet.hk2.config.Transaction;
import java.lang.reflect.Proxy;
/**
* TODO: fix the duplication of code for Property/SystemProperty by refactoring the config bean interfaces.
*/
public class ConfigToolsImpl extends AMXImplBase
{
private static void debug(final String s)
{
System.out.println(s);
}
public ConfigToolsImpl(final ObjectName parent)
{
super(parent, ConfigTools.class);
}
private static Property findProperty(final List<Property> props, final String name)
{
for (final Property prop : props)
{
if (prop == null)
{
debug("WARNING: null Property object in List<Property>");
continue;
}
if (prop.getName().equals(name))
{
return prop;
}
}
return null;
}
private static SystemProperty findSystemProperty(final List<SystemProperty> props, final String name)
{
for (final SystemProperty prop : props)
{
if (prop.getName().equals(name))
{
return prop;
}
}
return null;
}
/** base class for Property or SystemProperty support */
private static abstract class AnyPropsSetter implements ConfigCode
{
protected final List<Map mNewProps;
protected final boolean mClearAll;
AnyPropsSetter(List<Map newProps, final boolean clearAll)
{
mNewProps = newProps;
mClearAll = clearAll;
}
public Object run(ConfigBeanProxy... params)
throws PropertyVetoException, TransactionFailure
{
if (params.length != 1)
{
throw new IllegalArgumentException();
}
final ConfigBeanProxy parent = params[0];
final ConfigBean source = (ConfigBean) ConfigBean.unwrap(parent);
final ConfigSupport configSupport = source.getHabitat().getComponent(ConfigSupport.class);
return _run(parent, configSupport);
}
protected Set<String> propNames()
{
final Set<String> names = new HashSet();
for (final Map<String, String> newProp : mNewProps)
{
names.add( newProp.get("Name") );
}
return names;
}
abstract Object _run(final ConfigBeanProxy parent, final ConfigSupport spt) throws PropertyVetoException, TransactionFailure;
}
private static final class PropsSetter extends AnyPropsSetter
{
public PropsSetter(List<Map newProps, final boolean clearAll)
{
super(newProps, clearAll);
}
public Object _run(final ConfigBeanProxy parent, final ConfigSupport configSupport)
throws PropertyVetoException, TransactionFailure
{
final PropertyBag bag = (PropertyBag) parent;
final List<Property> props = bag.getProperty();
if ( mClearAll)
{
// remove all item that aren't in the new ones
final Set<String> newPropNames = propNames();
final List<Property> toRemove = new ArrayList();
for( final Property existing : props )
{
if ( ! newPropNames.contains(existing.getName()) )
{
toRemove.add(existing);
}
}
props.removeAll( toRemove );
}
final WriteableView parentW = (WriteableView)Proxy.getInvocationHandler(parent);
final Transaction t = parentW.getTransaction();
for (final Map<String, String> newProp : mNewProps)
{
final String name = newProp.get("Name");
final String value = newProp.get("Value");
final String description = newProp.get("Description");
Property prop = findProperty(props, name);
if (prop != null)
{
final Property propW = configSupport.getWriteableView(prop);
((WriteableView)Proxy.getInvocationHandler(propW)).join( t );
prop = propW;
//debug("Updated system-property: " + prop.getName());
}
else
{
prop = parent.createChild(Property.class);
props.add(prop);
//debug("Create new system-property: " + prop.getName());
}
prop.setName(name);
prop.setValue(value);
prop.setDescription(description);
}
return null;
}
}
private static final class SystemPropsSetter extends AnyPropsSetter
{
public SystemPropsSetter(List<Map newProps, final boolean clearAll)
{
super(newProps, clearAll);
}
public Object _run(final ConfigBeanProxy parent, final ConfigSupport configSupport)
throws PropertyVetoException, TransactionFailure
{
final SystemPropertyBag bag = (SystemPropertyBag) parent;
final List<SystemProperty> props = bag.getSystemProperty();
// it's important to *modify* existing items, not remove then and re-add them
if ( mClearAll)
{
// remove all item that aren't in the new ones
final Set<String> newPropNames = propNames();
final List<SystemProperty> toRemove = new ArrayList();
for( final SystemProperty existing : props )
{
if ( ! newPropNames.contains(existing.getName()) )
{
toRemove.add(existing);
}
}
props.removeAll( toRemove );
}
final WriteableView parentW = (WriteableView)Proxy.getInvocationHandler(parent);
final Transaction t = parentW.getTransaction();
for (final Map<String, String> newProp : mNewProps)
{
final String name = newProp.get("Name");
final String value = newProp.get("Value");
final String description = newProp.get("Description");
SystemProperty prop = findSystemProperty(props, name);
if (prop != null)
{
final SystemProperty propW = configSupport.getWriteableView(prop);
((WriteableView)Proxy.getInvocationHandler(propW)).join( t );
prop = propW;
//debug("Updated system-property: " + prop.getName());
}
else
{
prop = parent.createChild(SystemProperty.class);
props.add(prop);
//debug("Create new system-property: " + prop.getName());
}
prop.setName(name);
prop.setValue(value);
prop.setDescription(description);
}
return null;
}
}
/*
public void testSetProps() {
final List<Map props = new ArrayList
Other Glassfish examples (source code examples)
Here is a short list of links related to this Glassfish ConfigToolsImpl.java source code file:
|