objectstateutils.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,644 行 · 第 1/4 页
JAVA
1,644 行
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.axis2.util;
import org.apache.axis2.description.AxisMessage;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.AxisServiceGroup;
import org.apache.axis2.description.TransportInDescription;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.transport.TransportListener;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.xml.namespace.QName;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
/**
* Provides functions for saving and restoring an object's state.
*/
public class ObjectStateUtils {
/*
* setup for logging
*/
private static final Log log = LogFactory.getLog(ObjectStateUtils.class);
// used as part of the metadata written out
// indicating a null or empty object
public static boolean EMPTY_OBJECT = false;
// used as part of the metadata written out
// indicating a non-null or live object
public static boolean ACTIVE_OBJECT = true;
// used to indicate the end of a list
public static String LAST_ENTRY = "LAST_OBJ";
// used to indicate an "empty" object
public static String EMPTY_MARKER = "EMPTY_OBJ";
// used to indicate an valid "null" object,
// typically used in key-value pairs where a non-null key refers to a null
// value
public static String NULL_OBJECT = "NULL_OBJ";
// message/trace/logging strings
public static final String UNSUPPORTED_SUID = "Serialization version ID is not supported.";
public static final String UNSUPPORTED_REVID = "Revision ID is not supported.";
public static final String OBJ_SAVE_PROBLEM = "The object could not be saved to the output stream. The object may or may not be important for processing the message when it is restored. Look at how the object is to be used during message processing.";
public static final String OBJ_RESTORE_PROBLEM = "The object could not be restored from the input stream. The object may or may not be important for processing the message when it is restored. Look at how the object is to be used during message processing.";
// as a way to improve performance and reduce trace logging with
// extra exceptions, keep a table of classes that are not serializable
// and only log the first time it that the class is encountered in
// an NotSerializableException
// note that the Hashtable is synchronized by Java so we shouldn't need to
// do extra control over access to the table
public static Hashtable NotSerializableList = new Hashtable();
// --------------------------------------------------------------------
// Save/Restore methods
// --------------------------------------------------------------------
/**
* Write a string to the specified output stream. <p/> The format of the
* information written to the output stream is: <BOLD>Non-Null String</BOLD>
* <LI> UTF - class name string
* <LI> boolean - active flag
* <LI> Object - string data <p/> <BOLD>Null String</BOLD>
* <LI> UTF - description
* <LI> boolean - empty flag <p/>
*
* @param out
* The output stream
* @param str
* The string to write
* @param desc
* A text description to use for logging
* @throws IOException
* Exception
*/
public static void writeString(ObjectOutput out, String str, String desc)
throws IOException {
// The total number of bytes needed to represent all
// the characters of a string is calculated when the string
// is serialized. If this number is larger than 65535 (ie, 64 KB)
// then a java.io.UTFDataFormatException is thrown
if (str != null) {
String str_desc = str.getClass().getName();
// this string is expected to fit the writeUTF limitations
out.writeUTF(str_desc);
out.writeBoolean(ACTIVE_OBJECT);
out.writeObject(str);
// trace point
if (log.isTraceEnabled()) {
log
.trace("ObjectStateUtils:writeString(): ACTIVE string: str_desc ["
+ str_desc
+ "] string ["
+ str
+ "] desc [" + desc + "]");
}
} else {
// this string is expected to fit the writeUTF limitations
out.writeUTF(desc);
out.writeBoolean(EMPTY_OBJECT);
// for now, don't trace the EMPTY lines
// // trace point
// if (log.isTraceEnabled())
// {
// log.trace("ObjectStateUtils:writeString(): EMPTY String desc
// ["+desc+"] ");
// }
}
}
/**
* Read a string from the specified input stream. Returns null if no string
* is available. <p/> The format of the information to be read from the
* input stream should be <BOLD>Non-Null String</BOLD>
* <LI> UTF - class name string
* <LI> boolean - active flag
* <LI> Object - string data <p/> <BOLD>Null String</BOLD>
* <LI> UTF - description
* <LI> boolean - empty flag <p/>
*
* @param in
* The input stream
* @param desc
* A text description to use for logging
* @return The string or null, if not available
* @throws IOException
* @throws ClassNotFoundException
*/
public static String readString(ObjectInput in, String desc)
throws IOException, ClassNotFoundException {
String str = null;
// get the marker
String str_desc = in.readUTF();
// get the flag
boolean isActive = in.readBoolean();
if (isActive == ACTIVE_OBJECT) {
str = (String) in.readObject();
}
if (log.isTraceEnabled()) {
log.trace("ObjectStateUtils:readString(): [" + desc
+ "] returning [" + str + "] for saved [" + str_desc
+ "]");
}
return str;
}
/**
* Write an object to the specified output stream. <p/> The format of the
* information written to the output stream is <p/> <BOLD>Non-Null Object</BOLD>
* <LI> UTF - class name string
* <LI> boolean - active flag
* <LI> object - object if no error in the form of int byte array
*
* <LI> LAST_ENTRY marker in the form of int object <p/> <BOLD>Null Object</BOLD>
* <LI> UTF - description
* <LI> boolean - empty flag <p/>
*
* @param out
* The output stream
* @param obj
* The object to write
* @param desc
* A text description to use for logging
* @throws IOException
* Exception
*/
public static void writeObject(ObjectOutput out, Object obj, String desc)
throws IOException {
IOException returned_exception = null;
if (obj != null) {
String objClassName = obj.getClass().getName();
String fullDesc = desc + ":" + objClassName;
// this string is expected to fit the writeUTF limitations
out.writeUTF(fullDesc);
try {
// put the object into a test output buffer to see if it can be
// saved
// this technique preserves the integrity of the real output
// stream in the
// event of a serialization error
ByteArrayOutputStream test_outBuffer = new ByteArrayOutputStream();
ObjectOutputStream test_objOut = new ObjectOutputStream(
test_outBuffer);
// write the object to the test buffer
test_objOut.writeObject(obj);
test_objOut.close();
// put the contents of the test buffer into the
// real output stream
test_outBuffer.close();
byte[] data = test_outBuffer.toByteArray();
out.writeBoolean(ACTIVE_OBJECT);
out.writeObject(data);
} catch (NotSerializableException nse2) {
returned_exception = nse2;
// process this exception
traceNotSerializable(obj, nse2, desc,
"ObjectStateUtils.writeObject()", OBJ_SAVE_PROBLEM);
} catch (IOException exc2) {
// use this as a generic point for exceptions for the test
// output stream
returned_exception = exc2;
// trace point
if (log.isTraceEnabled()) {
log.trace("ObjectStateUtils:writeObject(): object["
+ obj.getClass().getName()
+ "] ***Exception*** ["
+ exc2.getClass().getName() + " : "
+ exc2.getMessage() + "] " + OBJ_SAVE_PROBLEM,
exc2);
// exc2.printStackTrace();
}
}
if (returned_exception != null) {
// Write a null object into the stream instead of the data that
// failed
out.writeBoolean(EMPTY_OBJECT);
// let the caller know that there was a problem
// note the integrity of the real output stream has been
// preserved
throw returned_exception;
}
} else {
// this string is expected to fit the writeUTF limitations
out.writeUTF(desc);
out.writeBoolean(EMPTY_OBJECT);
// trace point
if (log.isTraceEnabled()) {
log.trace("ObjectStateUtils:writeObject(): EMPTY Object ["
+ desc + "] ");
}
}
}
/**
* Read an object from the specified input stream. Returns null if no object
* is available. <p/> The format of the information to be read from the
* input stream should be <BOLD>Non-Null Object</BOLD>
* <LI> UTF - class name string
* <LI> boolean - active flag
* <LI> object - object if no error
* <LI> LAST_ENTRY marker <p/> <BOLD>Null Object</BOLD>
* <LI> UTF - description
* <LI> boolean - empty flag <p/>
*
* @param in
* The input stream
* @param desc
* A text description to use for logging
* @return The object or null, if not available
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object readObject(ObjectInput in, String desc)
throws IOException, ClassNotFoundException {
Object obj = null;
byte[] data = null;
String str_desc = in.readUTF();
boolean isActive = in.readBoolean();
if (isActive == ACTIVE_OBJECT) {
// Read the byte array that contains our object
data = (byte[]) in.readObject();
// convert the byte[] back into the real object
ByteArrayInputStream test_inBuffer = new ByteArrayInputStream(data);
ObjectInputStream test_objIn = new ObjectInputStream(test_inBuffer);
obj = test_objIn.readObject();
test_objIn.close();
test_inBuffer.close();
}
String value = "null";
if (obj != null) {
value = obj.getClass().getName();
}
// trace point
if (log.isTraceEnabled()) {
log.trace("ObjectStateUtils:readObject(): [" + desc
+ "] returning [" + value + "] for saved [" + str_desc
+ "]");
}
return obj;
}
/**
* Write an array of objects to the specified output stream. <p/> The format
* of the information written to the output stream is
* <LI> class name of the array
* <LI> active or empty
* <LI> data <p/> NOTE: each object in the array should implement either
* java.io.Serializable or java.io.Externalizable in order to be saved <p/>
*
* @param out
* The output stream
* @param al
* The ArrayList to write
* @param desc
* A text description to use for logging
* @throws IOException
* Exception
*/
public static void writeArrayList(ObjectOutput out, ArrayList al,
String desc) throws IOException {
// The format of the data is
//
// Non-null list:
// UTF - description string
// boolean - active flag
// objects - objects from list
// - ACTIVE_OBJECT
// - data
// EMPTY_OBJEXT - end of array marker
//
// Null list:
// UTF - description string
// boolean - empty flag
//
int savedListSize = 0;
out.writeUTF(desc);
out.writeBoolean(al == null ? EMPTY_OBJECT : ACTIVE_OBJECT);
if (al != null) {
// setup an iterator for the list
Iterator i = al.iterator();
while (i.hasNext()) {
Object obj = i.next();
try {
// put each list entry into a test output buffer to see if
// it can be saved
// this technique preserves the integrity of the real output
// stream in the
// event of a serialization error
ByteArrayOutputStream test_outBuffer = new ByteArrayOutputStream();
ObjectOutputStream test_objOut = new ObjectOutputStream(
test_outBuffer);
// write the object to the test buffer
test_objOut.writeObject(obj);
test_objOut.flush();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?