Search in sources :

Example 1 with JavaSerializer

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();
}
Also used : Serializable(java.io.Serializable) ByteArrayInputStream(java.io.ByteArrayInputStream) JavaSerializer(org.apache.wicket.serialize.java.JavaSerializer) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 2 with JavaSerializer

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);
        }
    });
}
Also used : IPageStore(org.apache.wicket.pageStore.IPageStore) IPageManager(org.apache.wicket.page.IPageManager) PageStoreManager(org.apache.wicket.page.PageStoreManager) DefaultButtonImageResource(org.apache.wicket.markup.html.image.resource.DefaultButtonImageResource) HttpSessionDataStore(org.apache.wicket.pageStore.memory.HttpSessionDataStore) PageNumberEvictionStrategy(org.apache.wicket.pageStore.memory.PageNumberEvictionStrategy) JavaSerializer(org.apache.wicket.serialize.java.JavaSerializer) IPageManagerContext(org.apache.wicket.page.IPageManagerContext) DefaultPageManagerProvider(org.apache.wicket.DefaultPageManagerProvider) IDataStore(org.apache.wicket.pageStore.IDataStore) DefaultPageStore(org.apache.wicket.pageStore.DefaultPageStore)

Example 3 with JavaSerializer

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;
    }
}
Also used : JavaSerializer(org.apache.wicket.serialize.java.JavaSerializer) ISerializer(org.apache.wicket.serialize.ISerializer)

Example 4 with JavaSerializer

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);
}
Also used : IPageStore(org.apache.wicket.pageStore.IPageStore) DummyPageManagerContext(org.apache.wicket.pageStore.memory.DummyPageManagerContext) JavaSerializer(org.apache.wicket.serialize.java.JavaSerializer) IDataStore(org.apache.wicket.pageStore.IDataStore) InMemoryPageStore(org.apache.wicket.versioning.InMemoryPageStore) DefaultPageStore(org.apache.wicket.pageStore.DefaultPageStore)

Example 5 with JavaSerializer

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);
                }
            };
        }
    };
}
Also used : IPageManager(org.apache.wicket.page.IPageManager) PageStoreManager(org.apache.wicket.page.PageStoreManager) AsynchronousDataStore(org.apache.wicket.pageStore.AsynchronousDataStore) WicketTester(org.apache.wicket.util.tester.WicketTester) IPageManagerProvider(org.apache.wicket.IPageManagerProvider) JavaSerializer(org.apache.wicket.serialize.java.JavaSerializer) IPageManagerContext(org.apache.wicket.page.IPageManagerContext) IDataStore(org.apache.wicket.pageStore.IDataStore) DefaultPageStore(org.apache.wicket.pageStore.DefaultPageStore) Before(org.junit.Before)

Aggregations

JavaSerializer (org.apache.wicket.serialize.java.JavaSerializer)11 DefaultPageStore (org.apache.wicket.pageStore.DefaultPageStore)5 IPageManager (org.apache.wicket.page.IPageManager)4 IPageManagerContext (org.apache.wicket.page.IPageManagerContext)4 PageStoreManager (org.apache.wicket.page.PageStoreManager)4 IDataStore (org.apache.wicket.pageStore.IDataStore)4 IPageStore (org.apache.wicket.pageStore.IPageStore)4 Test (org.junit.Test)4 ObjectOutputStream (java.io.ObjectOutputStream)2 OutputStream (java.io.OutputStream)2 DefaultPageManagerProvider (org.apache.wicket.DefaultPageManagerProvider)2 IPageManagerProvider (org.apache.wicket.IPageManagerProvider)2 DefaultButtonImageResource (org.apache.wicket.markup.html.image.resource.DefaultButtonImageResource)2 HttpSessionDataStore (org.apache.wicket.pageStore.memory.HttpSessionDataStore)2 PageNumberEvictionStrategy (org.apache.wicket.pageStore.memory.PageNumberEvictionStrategy)2 WicketTester (org.apache.wicket.util.tester.WicketTester)2 InMemoryPageStore (org.apache.wicket.versioning.InMemoryPageStore)2 Before (org.junit.Before)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1