001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.policy.jaxws.xmlstreamwriter;
038:
039: import com.sun.xml.ws.policy.jaxws.privateutil.LocalizationMessages;
040: import com.sun.xml.ws.policy.privateutil.PolicyLogger;
041: import java.lang.reflect.InvocationHandler;
042: import java.lang.reflect.Method;
043: import java.lang.reflect.Proxy;
044: import javax.xml.stream.XMLStreamException;
045: import javax.xml.stream.XMLStreamWriter;
046:
047: /**
048: * The class provides an implementation of an {@link InvocationHandler} interface
049: * that handles requests of {@link XMLStreamWriter} proxy instances.
050: *<p/>
051: * This {@link InvocationHandler} implementation adds additional feature or enhancement
052: * to the underlying {@link XMLStreamWriter} instance. The new enhancement or feature is
053: * defined by an {@link InvocationProcessor} implementation.
054: * <p/>
055: * The class also contains a static factory method for creating such 'enhanced'
056: * {@link XMLStreamWriter} proxies.
057: *
058: * @author Marek Potociar (marek.potociar at sun.com)
059: */
060: public final class EnhancedXmlStreamWriterProxy implements
061: InvocationHandler {
062: private static final PolicyLogger LOGGER = PolicyLogger
063: .getLogger(EnhancedXmlStreamWriterProxy.class);
064:
065: private static final Class<?>[] PROXIED_INTERFACES = new Class<?>[] { XMLStreamWriter.class };
066:
067: // preloaded Method objects for the methods in java.lang.Object
068: private static final Method hashCodeMethod;
069: private static final Method equalsMethod;
070: private static final Method toStringMethod;
071: static {
072: try {
073: hashCodeMethod = Object.class.getMethod("hashCode");
074: equalsMethod = Object.class.getMethod("equals",
075: new Class[] { Object.class });
076: toStringMethod = Object.class.getMethod("toString");
077: } catch (NoSuchMethodException e) {
078: throw LOGGER.logSevereException(new NoSuchMethodError(e
079: .getMessage()), e);
080: }
081: }
082:
083: // invocation procesor that processes
084: private final InvocationProcessor invocationProcessor;
085:
086: /**
087: * Creates a wrapper {@link XMLStreamWriter} proxy that adds enhanced feature
088: * to the {@code writer} instance.
089: *
090: * @param writer {@link XMLStreamWriter} instance that should be enhanced with
091: * content filtering feature.
092: * @param processorFactory {@link InvocationProcessorFactory} instance that
093: * is used to create {@link InvocationProcessor} which implements new enhancement
094: * or feature.
095: *
096: * @return new enhanced {XMLStreamWriter} (proxy) instance
097: * @throws XMLStreamException in case of any problems with creating the proxy
098: */
099: public static XMLStreamWriter createProxy(
100: final XMLStreamWriter writer,
101: final InvocationProcessorFactory processorFactory)
102: throws XMLStreamException {
103: LOGGER.entering();
104:
105: XMLStreamWriter proxy = null;
106: try {
107: proxy = (XMLStreamWriter) Proxy.newProxyInstance(writer
108: .getClass().getClassLoader(), PROXIED_INTERFACES,
109: new EnhancedXmlStreamWriterProxy(writer,
110: processorFactory));
111:
112: return proxy;
113: } finally {
114: LOGGER.exiting(proxy);
115: }
116: }
117:
118: private EnhancedXmlStreamWriterProxy(final XMLStreamWriter writer,
119: final InvocationProcessorFactory processorFactory)
120: throws XMLStreamException {
121: this .invocationProcessor = processorFactory
122: .createInvocationProcessor(writer);
123: }
124:
125: public Object invoke(final Object proxy, final Method method,
126: final Object[] args) throws Throwable {
127: if (LOGGER.isMethodCallLoggable()) {
128: LOGGER.entering(method, args);
129: }
130:
131: Object result = null;
132: try {
133: final Class declaringClass = method.getDeclaringClass();
134: if (declaringClass == Object.class) {
135: return handleObjectMethodCall(proxy, method, args);
136: } else {
137: final Invocation invocation = Invocation
138: .createInvocation(method, args);
139: result = invocationProcessor.process(invocation);
140: return result;
141: }
142: } finally {
143: LOGGER.exiting(result);
144: }
145: }
146:
147: private Object handleObjectMethodCall(final Object proxy,
148: final Method method, final Object[] args) {
149: if (method.equals(hashCodeMethod)) {
150: return new Integer(System.identityHashCode(proxy));
151: } else if (method.equals(equalsMethod)) {
152: return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
153: } else if (method.equals(toStringMethod)) {
154: return proxy.getClass().getName() + '@'
155: + Integer.toHexString(proxy.hashCode());
156: } else {
157: throw LOGGER
158: .logSevereException(new InternalError(
159: LocalizationMessages
160: .WSP_1007_UNEXPECTED_OBJECT_METHOD(method)));
161: }
162: }
163: }
|