
it.tidalwave.netbeans.indexeddataobject.impl.IndexedDataObjectReplace Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* OpenBlueSky - NetBeans Platform Enhancements
* Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* WWW: http://openbluesky.java.net
* SCM: https://bitbucket.org/tidalwave/openbluesky-src
*
**********************************************************************************************************************/
package it.tidalwave.netbeans.indexeddataobject.impl;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import org.openide.loaders.DataObject;
import it.tidalwave.util.Id;
import it.tidalwave.role.Identifiable;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.netbeans.indexeddataobject.IndexedDataObjectManager;
/***********************************************************************************************************************
*
* The most appropriate long-term persistence for IndexedDataObjects is their
* id, while DataObjects serialize the primary file (furthermore primary
* files, when serialized, lose their id, so this stuff is really needed).
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class IndexedDataObjectReplace implements Serializable
{
private static final long serialVersionUID = 4049595044948243058L;
private static final IndexedDataObjectManager indexedDataObjectManager = Locator.find(IndexedDataObjectManager.class);
@Nonnull
private Id id;
@CheckForNull
private transient DataObject dataObject;
public IndexedDataObjectReplace (final DataObject dataObject)
{
this.dataObject = dataObject;
}
@PostConstruct
private void initialize()
{
this.id = dataObject.getLookup().lookup(Identifiable.class).getId();
}
@Nonnull
public Object readResolve()
{
return dataObject;
}
private void readObject (final @Nonnull ObjectInputStream ois)
throws IOException, ClassNotFoundException
{
ois.defaultReadObject();
if (id == null)
{
throw new FileNotFoundException();
}
dataObject = indexedDataObjectManager.findById(id);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy