0N/A/*
2362N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.beans;
0N/A
1405N/Aimport com.sun.beans.finder.PersistenceDelegateFinder;
1405N/A
0N/Aimport java.util.HashMap;
0N/Aimport java.util.IdentityHashMap;
0N/Aimport java.util.Map;
0N/A
0N/A/**
0N/A * An <code>Encoder</code> is a class which can be used to create
0N/A * files or streams that encode the state of a collection of
0N/A * JavaBeans in terms of their public APIs. The <code>Encoder</code>,
0N/A * in conjunction with its persistence delegates, is responsible for
0N/A * breaking the object graph down into a series of <code>Statements</code>s
0N/A * and <code>Expression</code>s which can be used to create it.
0N/A * A subclass typically provides a syntax for these expressions
0N/A * using some human readable form - like Java source code or XML.
0N/A *
0N/A * @since 1.4
0N/A *
0N/A * @author Philip Milne
0N/A */
0N/A
0N/Apublic class Encoder {
1405N/A private final PersistenceDelegateFinder finder = new PersistenceDelegateFinder();
0N/A private Map bindings = new IdentityHashMap();
0N/A private ExceptionListener exceptionListener;
0N/A boolean executeStatements = true;
0N/A private Map attributes;
0N/A
0N/A /**
0N/A * Write the specified object to the output stream.
0N/A * The serialized form will denote a series of
0N/A * expressions, the combined effect of which will create
0N/A * an equivalent object when the input stream is read.
0N/A * By default, the object is assumed to be a <em>JavaBean</em>
0N/A * with a nullary constructor, whose state is defined by
0N/A * the matching pairs of "setter" and "getter" methods
0N/A * returned by the Introspector.
0N/A *
0N/A * @param o The object to be written to the stream.
0N/A *
0N/A * @see XMLDecoder#readObject
0N/A */
0N/A protected void writeObject(Object o) {
0N/A if (o == this) {
0N/A return;
0N/A }
0N/A PersistenceDelegate info = getPersistenceDelegate(o == null ? null : o.getClass());
0N/A info.writeObject(o, this);
0N/A }
0N/A
0N/A /**
0N/A * Sets the exception handler for this stream to <code>exceptionListener</code>.
0N/A * The exception handler is notified when this stream catches recoverable
0N/A * exceptions.
0N/A *
0N/A * @param exceptionListener The exception handler for this stream;
0N/A * if <code>null</code> the default exception listener will be used.
0N/A *
0N/A * @see #getExceptionListener
0N/A */
0N/A public void setExceptionListener(ExceptionListener exceptionListener) {
0N/A this.exceptionListener = exceptionListener;
0N/A }
0N/A
0N/A /**
0N/A * Gets the exception handler for this stream.
0N/A *
0N/A * @return The exception handler for this stream;
0N/A * Will return the default exception listener if this has not explicitly been set.
0N/A *
0N/A * @see #setExceptionListener
0N/A */
0N/A public ExceptionListener getExceptionListener() {
0N/A return (exceptionListener != null) ? exceptionListener : Statement.defaultExceptionListener;
0N/A }
0N/A
0N/A Object getValue(Expression exp) {
0N/A try {
0N/A return (exp == null) ? null : exp.getValue();
0N/A }
0N/A catch (Exception e) {
0N/A getExceptionListener().exceptionThrown(e);
0N/A throw new RuntimeException("failed to evaluate: " + exp.toString());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the persistence delegate for the given type.
2144N/A * The persistence delegate is calculated by applying
2144N/A * the following rules in order:
2144N/A * <ol>
2144N/A * <li>
2144N/A * If a persistence delegate is associated with the given type
2144N/A * by using the {@link #setPersistenceDelegate} method
2144N/A * it is returned.
0N/A * <li>
2144N/A * A persistence delegate is then looked up by the name
2144N/A * composed of the the fully qualified name of the given type
2144N/A * and the "PersistenceDelegate" postfix.
2144N/A * For example, a persistence delegate for the {@code Bean} class
2144N/A * should be named {@code BeanPersistenceDelegate}
2144N/A * and located in the same package.
2144N/A * <pre>
2144N/A * public class Bean { ... }
2144N/A * public class BeanPersistenceDelegate { ... }</pre>
2144N/A * The instance of the {@code BeanPersistenceDelegate} class
2144N/A * is returned for the {@code Bean} class.
2144N/A * <li>
2144N/A * If the type is {@code null},
2144N/A * a shared internal persistence delegate is returned
2144N/A * that encodes {@code null} value.
0N/A * <li>
2144N/A * If the type is a {@code enum} declaration,
2144N/A * a shared internal persistence delegate is returned
2144N/A * that encodes constants of this enumeration
2144N/A * by their names.
2144N/A * <li>
2144N/A * If the type is a primitive type or the corresponding wrapper,
2144N/A * a shared internal persistence delegate is returned
2144N/A * that encodes values of the given type.
2144N/A * <li>
2144N/A * If the type is an array,
2144N/A * a shared internal persistence delegate is returned
2144N/A * that encodes an array of the appropriate type and length,
2144N/A * and each of its elements as if they are properties.
0N/A * <li>
2144N/A * If the type is a proxy,
2144N/A * a shared internal persistence delegate is returned
2144N/A * that encodes a proxy instance by using
2144N/A * the {@link java.lang.reflect.Proxy#newProxyInstance} method.
0N/A * <li>
2144N/A * If the {@link BeanInfo} for this type has a {@link BeanDescriptor}
2144N/A * which defined a "persistenceDelegate" attribute,
2144N/A * the value of this named attribute is returned.
2144N/A * <li>
2144N/A * In all other cases the default persistence delegate is returned.
2144N/A * The default persistence delegate assumes the type is a <em>JavaBean</em>,
2144N/A * implying that it has a default constructor and that its state
2144N/A * may be characterized by the matching pairs of "setter" and "getter"
2144N/A * methods returned by the {@link Introspector} class.
0N/A * The default constructor is the constructor with the greatest number
0N/A * of parameters that has the {@link ConstructorProperties} annotation.
2144N/A * If none of the constructors has the {@code ConstructorProperties} annotation,
0N/A * then the nullary constructor (constructor with no parameters) will be used.
2144N/A * For example, in the following code fragment, the nullary constructor
2144N/A * for the {@code Foo} class will be used,
2144N/A * while the two-parameter constructor
2144N/A * for the {@code Bar} class will be used.
2144N/A * <pre>
2144N/A * public class Foo {
0N/A * public Foo() { ... }
0N/A * public Foo(int x) { ... }
2144N/A * }
2144N/A * public class Bar {
0N/A * public Bar() { ... }
0N/A * &#64;ConstructorProperties({"x"})
0N/A * public Bar(int x) { ... }
0N/A * &#64;ConstructorProperties({"x", "y"})
0N/A * public Bar(int x, int y) { ... }
2144N/A * }</pre>
2144N/A * </ol>
0N/A *
2144N/A * @param type the class of the objects
2144N/A * @return the persistence delegate for the given type
0N/A *
0N/A * @see #setPersistenceDelegate
0N/A * @see java.beans.Introspector#getBeanInfo
0N/A * @see java.beans.BeanInfo#getBeanDescriptor
0N/A */
0N/A public PersistenceDelegate getPersistenceDelegate(Class<?> type) {
2535N/A PersistenceDelegate pd = this.finder.find(type);
4348N/A if (pd == null) {
4348N/A pd = MetaData.getPersistenceDelegate(type);
4348N/A if (pd != null) {
4348N/A this.finder.register(type, pd);
4348N/A }
4348N/A }
4348N/A return pd;
0N/A }
0N/A
0N/A /**
2144N/A * Associates the specified persistence delegate with the given type.
0N/A *
2144N/A * @param type the class of objects that the specified persistence delegate applies to
2144N/A * @param delegate the persistence delegate for instances of the given type
0N/A *
0N/A * @see #getPersistenceDelegate
0N/A * @see java.beans.Introspector#getBeanInfo
0N/A * @see java.beans.BeanInfo#getBeanDescriptor
0N/A */
2144N/A public void setPersistenceDelegate(Class<?> type, PersistenceDelegate delegate) {
2535N/A this.finder.register(type, delegate);
0N/A }
0N/A
0N/A /**
0N/A * Removes the entry for this instance, returning the old entry.
0N/A *
0N/A * @param oldInstance The entry that should be removed.
0N/A * @return The entry that was removed.
0N/A *
0N/A * @see #get
0N/A */
0N/A public Object remove(Object oldInstance) {
0N/A Expression exp = (Expression)bindings.remove(oldInstance);
0N/A return getValue(exp);
0N/A }
0N/A
0N/A /**
0N/A * Returns a tentative value for <code>oldInstance</code> in
0N/A * the environment created by this stream. A persistence
0N/A * delegate can use its <code>mutatesTo</code> method to
0N/A * determine whether this value may be initialized to
0N/A * form the equivalent object at the output or whether
0N/A * a new object must be instantiated afresh. If the
0N/A * stream has not yet seen this value, null is returned.
0N/A *
0N/A * @param oldInstance The instance to be looked up.
0N/A * @return The object, null if the object has not been seen before.
0N/A */
0N/A public Object get(Object oldInstance) {
0N/A if (oldInstance == null || oldInstance == this ||
0N/A oldInstance.getClass() == String.class) {
0N/A return oldInstance;
0N/A }
0N/A Expression exp = (Expression)bindings.get(oldInstance);
0N/A return getValue(exp);
0N/A }
0N/A
0N/A private Object writeObject1(Object oldInstance) {
0N/A Object o = get(oldInstance);
0N/A if (o == null) {
0N/A writeObject(oldInstance);
0N/A o = get(oldInstance);
0N/A }
0N/A return o;
0N/A }
0N/A
0N/A private Statement cloneStatement(Statement oldExp) {
0N/A Object oldTarget = oldExp.getTarget();
0N/A Object newTarget = writeObject1(oldTarget);
0N/A
0N/A Object[] oldArgs = oldExp.getArguments();
0N/A Object[] newArgs = new Object[oldArgs.length];
0N/A for (int i = 0; i < oldArgs.length; i++) {
0N/A newArgs[i] = writeObject1(oldArgs[i]);
0N/A }
1406N/A Statement newExp = Statement.class.equals(oldExp.getClass())
1406N/A ? new Statement(newTarget, oldExp.getMethodName(), newArgs)
1406N/A : new Expression(newTarget, oldExp.getMethodName(), newArgs);
1406N/A newExp.loader = oldExp.loader;
1406N/A return newExp;
0N/A }
0N/A
0N/A /**
0N/A * Writes statement <code>oldStm</code> to the stream.
0N/A * The <code>oldStm</code> should be written entirely
0N/A * in terms of the callers environment, i.e. the
0N/A * target and all arguments should be part of the
0N/A * object graph being written. These expressions
0N/A * represent a series of "what happened" expressions
0N/A * which tell the output stream how to produce an
0N/A * object graph like the original.
0N/A * <p>
0N/A * The implementation of this method will produce
0N/A * a second expression to represent the same expression in
0N/A * an environment that will exist when the stream is read.
0N/A * This is achieved simply by calling <code>writeObject</code>
0N/A * on the target and all the arguments and building a new
0N/A * expression with the results.
0N/A *
0N/A * @param oldStm The expression to be written to the stream.
0N/A */
0N/A public void writeStatement(Statement oldStm) {
0N/A // System.out.println("writeStatement: " + oldExp);
0N/A Statement newStm = cloneStatement(oldStm);
0N/A if (oldStm.getTarget() != this && executeStatements) {
0N/A try {
0N/A newStm.execute();
0N/A } catch (Exception e) {
0N/A getExceptionListener().exceptionThrown(new Exception("Encoder: discarding statement "
0N/A + newStm, e));
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The implementation first checks to see if an
0N/A * expression with this value has already been written.
0N/A * If not, the expression is cloned, using
0N/A * the same procedure as <code>writeStatement</code>,
0N/A * and the value of this expression is reconciled
0N/A * with the value of the cloned expression
0N/A * by calling <code>writeObject</code>.
0N/A *
0N/A * @param oldExp The expression to be written to the stream.
0N/A */
0N/A public void writeExpression(Expression oldExp) {
0N/A // System.out.println("Encoder::writeExpression: " + oldExp);
0N/A Object oldValue = getValue(oldExp);
0N/A if (get(oldValue) != null) {
0N/A return;
0N/A }
0N/A bindings.put(oldValue, (Expression)cloneStatement(oldExp));
0N/A writeObject(oldValue);
0N/A }
0N/A
0N/A void clear() {
0N/A bindings.clear();
0N/A }
0N/A
0N/A // Package private method for setting an attributes table for the encoder
0N/A void setAttribute(Object key, Object value) {
0N/A if (attributes == null) {
0N/A attributes = new HashMap();
0N/A }
0N/A attributes.put(key, value);
0N/A }
0N/A
0N/A Object getAttribute(Object key) {
0N/A if (attributes == null) {
0N/A return null;
0N/A }
0N/A return attributes.get(key);
0N/A }
0N/A}