use of org.apache.wicket.serialize.java.JavaSerializer in project wicket by apache.
the class PersistentPageManagerTest method serializationOutsideWicketLifecyle.
/**
* WICKET-3470
*
* Tests that a page already put in the session (in SessionEntry) can be serialized and later
* deserialized without the need of {@link IPageStore}
*
* @throws ClassNotFoundException
* @throws IOException
*/
@Test
public void serializationOutsideWicketLifecyle() throws IOException, ClassNotFoundException {
// make sure no leaked threadlocals are present
ThreadContext.detach();
// create IPageManager (with IPageStore) and store a page instance
IPageManager pageManager = newPersistentPageManager(APP_NAME);
TestPage toSerializePage = new TestPage();
pageManager.touchPage(toSerializePage);
pageManager.commitRequest();
// get the stored SessionEntry
Serializable sessionEntry = pageManager.getContext().getSessionAttribute(null);
// destroy the manager and the store
pageManager.destroy();
// simulate persisting of the http sessions initiated by the web container
byte[] serializedSessionEntry = new JavaSerializer(APP_NAME).serialize(sessionEntry);
assertNotNull("Wicket needs to be able to serialize the session entry", serializedSessionEntry);
// simulate loading of the persisted http session initiated by the web container
// when starting an application
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedSessionEntry));
// WicketFilter is not initialized so there is no Application available yet
Assert.assertFalse("Worker thread should be unaware of Wicket application", Application.exists());
assertEquals(APP_NAME, in.readObject());
// without available IPageStore the read SessionEntry holds
// the IManageablePage itself, not SerializedPage
Serializable loadedSessionEntry = (Serializable) in.readObject();
assertNotNull("Wicket needs to be able to deserialize the session entry regardless the application availability", loadedSessionEntry);
// provide new IPageStore which will read IManageablePage's or SerializedPage's
// from the SessionEntry's
IPageManager newPageManager = newPersistentPageManager(APP_NAME);
newPageManager.getContext().setSessionAttribute(null, loadedSessionEntry);
TestPage deserializedPage = (TestPage) newPageManager.getPage(toSerializePage.getPageId());
assertNotNull(deserializedPage);
assertEquals(toSerializePage.instanceID, deserializedPage.instanceID);
newPageManager.destroy();
}
use of org.apache.wicket.serialize.java.JavaSerializer in project wicket by apache.
the class TestApp1 method init.
@Override
protected void init() {
getSharedResources().add("cancelButton", new DefaultButtonImageResource("Cancel"));
setPageManagerProvider(new DefaultPageManagerProvider(this) {
@Override
public IPageManager get(IPageManagerContext pageManagerContext) {
IDataStore dataStore = new HttpSessionDataStore(pageManagerContext, new PageNumberEvictionStrategy(100));
IPageStore pageStore = new DefaultPageStore(new JavaSerializer(getApplicationKey()), dataStore, getStoreSettings().getInmemoryCacheSize());
return new PageStoreManager(getName(), pageStore, pageManagerContext);
}
});
}
use of org.apache.wicket.serialize.java.JavaSerializer in project wicket by apache.
the class WicketObjects method cloneObject.
/**
* Makes a deep clone of an object by serializing and deserializing it. The object must be fully
* serializable to be cloned. No extra debug info is gathered.
*
* @param object
* The object to clone
* @return A deep copy of the object
* @see #cloneModel(Object)
*/
public static <T> T cloneObject(final T object) {
if (object == null) {
return null;
} else {
ISerializer serializer = null;
if (Application.exists()) {
serializer = Application.get().getFrameworkSettings().getSerializer();
}
if (serializer == null || serializer instanceof JavaSerializer) {
// WICKET-6334 create a new instance of JavaSerializer that doesn't use custom IObjectCheckers
serializer = new JavaSerializer(SerializingObjectSizeOfStrategy.class.getName());
}
byte[] serialized = serializer.serialize(object);
if (serialized == null) {
throw new IllegalStateException("A problem occurred while serializing an object. " + "Please check the earlier logs for more details. Problematic object: " + object);
}
Object deserialized = serializer.deserialize(serialized);
return (T) deserialized;
}
}
use of org.apache.wicket.serialize.java.JavaSerializer in project wicket by apache.
the class PersistentPageManagerTest method newPersistentPageManager.
private PageStoreManager newPersistentPageManager(String appName) {
IDataStore dataStore = new InMemoryPageStore();
IPageStore pageStore = new DefaultPageStore(new JavaSerializer(appName), dataStore, 4);
IPageManagerContext pageManagerContext = new DummyPageManagerContext();
return new PageStoreManager(appName, pageStore, pageManagerContext);
}
use of org.apache.wicket.serialize.java.JavaSerializer in project wicket by apache.
the class PageVersioningTest method setup.
/**
* setup()
*/
@Before
public void setup() {
final PageVersioningApplication application = new PageVersioningApplication();
wicketTester = new WicketTester(application) {
/**
* @see org.apache.wicket.util.tester.BaseWicketTester#newTestPageManagerProvider()
*/
@Override
protected IPageManagerProvider newTestPageManagerProvider() {
return new IPageManagerProvider() {
@Override
public IPageManager apply(IPageManagerContext pageManagerContext) {
final IDataStore dataStore = new InMemoryPageStore();
final AsynchronousDataStore asyncDS = new AsynchronousDataStore(dataStore, 100);
final DefaultPageStore pageStore = new DefaultPageStore(new JavaSerializer(application.getApplicationKey()), asyncDS, 40);
return new PageStoreManager(application.getName(), pageStore, pageManagerContext);
}
};
}
};
}
Aggregations