de.javakaffee.web.msm.JavaSerializationTranscoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of memcached-session-manager Show documentation
Show all versions of memcached-session-manager Show documentation
The msm core, provides java serialization strategy.
/*
* Copyright 2009 Martin Grotzke
*
* 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 de.javakaffee.web.msm;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.catalina.session.StandardSession;
import org.apache.catalina.util.CustomObjectInputStream;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import de.javakaffee.web.msm.MemcachedSessionService.SessionManager;
/**
* A {@link SessionAttributesTranscoder} that serializes catalina {@link StandardSession}s using
* java serialization (and the serialization logic of {@link StandardSession} as
* found in {@link StandardSession#writeObjectData(ObjectOutputStream)} and
* {@link StandardSession#readObjectData(ObjectInputStream)}).
*
* @author Martin Grotzke
* @version $Id$
*/
public class JavaSerializationTranscoder implements SessionAttributesTranscoder {
private static final Log LOG = LogFactory.getLog( JavaSerializationTranscoder.class );
private static final String EMPTY_ARRAY[] = new String[0];
/**
* The dummy attribute value serialized when a NotSerializableException is
* encountered in writeObject()
.
*/
protected static final String NOT_SERIALIZED = "___NOT_SERIALIZABLE_EXCEPTION___";
private final SessionManager _manager;
/**
* Constructor.
*
* @param manager
* the manager
*/
public JavaSerializationTranscoder() {
this( null );
}
/**
* Constructor.
*
* @param manager
* the manager
*/
public JavaSerializationTranscoder( final SessionManager manager ) {
_manager = manager;
}
/**
* {@inheritDoc}
*/
@Override
public byte[] serializeAttributes( final MemcachedBackupSession session, final ConcurrentMap attributes ) {
if ( attributes == null ) {
throw new NullPointerException( "Can't serialize null" );
}
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
try {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream( bos );
writeAttributes( session, attributes, oos );
return bos.toByteArray();
} catch ( final IOException e ) {
throw new IllegalArgumentException( "Non-serializable object", e );
} finally {
closeSilently( bos );
closeSilently( oos );
}
}
private void writeAttributes( final MemcachedBackupSession session, final Map attributes,
final ObjectOutputStream oos ) throws IOException {
// Accumulate the names of serializable and non-serializable attributes
final String keys[] = attributes.keySet().toArray( EMPTY_ARRAY );
final List saveNames = new ArrayList();
final List