ExternalResource.java
/*
** Module : ExternalResource.java
** Abstract : instances of this class represent resources which associate with external
** applications.
**
** Copyright (c) 2017-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------Description----------------------------------
** 001 CA 20170228 Created initial version.
** 002 IAS 20200922 Get rid of possible NPE on serialization.
** 003 CA 20240918 Removed default methods from interfaces and moved them as concrete implementations, as
** default methods cause the Java metaspace to spike one order of magnitude.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.util;
import static com.goldencode.util.NativeTypeSerializer.*;
import java.io.*;
/**
* Instances of this class are helpers to associate a surrogate handle ID with a resource on a
* remote side. Useful for i.e. SUBSCRIBE/UNSUBSCRIBE/PUBLISH APIs.
*/
public class ExternalResource
implements Deletable,
WrappedResource,
Externalizable
{
/** The resource ID. */
private Long id;
/**
* Reports if this object is valid for use.
*
* @return Always <code>true</code>.
*/
@Override
public boolean valid()
{
return true;
}
/**
* Reports if this object is unknown.
*
* @return Always <code>false</code>.
*/
@Override
public boolean unknown()
{
return false;
}
/**
* Get this resource's ID.
*
* @return See above.
*/
@Override
public Long id()
{
return id;
}
/**
* Set this resource's ID.
*
* @param id
* The resource's ID.
*/
@Override
public void id(long id)
{
this.id = id;
}
/**
* Perform actual delete of an resource. At the time of this call, it is assumed the resource
* is valid for deletion (the handle and the resource are both valid).
*/
@Override
public void delete()
{
ProcedureManager.cleanupSubscriptions(this);
// the resource was deleted, clean up after it
handle.removeResource(this);
}
/**
* Check if the resource can veto the delete or not, before attempting the {@link #delete()} call.
*
* @param ref
* The exact handle holding the reference, on which the delete is attempted.
* @param explicit
* Flag indicating this is called via <code>DELETE OBJECT</code> from the application.
*
* @return <code>false</code> if the delete is vetoed.
*/
@Override
public boolean allowDelete(handle ref, boolean explicit)
{
return true;
}
/**
* Check if this instance equals the given one.
*
* @param obj
* The object to check.
*
* @return <code>true</code> if <code>obj</code> is a {@link ExternalResource} and its
* {@link #id} is the same as the one in this instance.
*/
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof ExternalResource))
{
return false;
}
return ((ExternalResource) obj).id == id;
}
/**
* Compute the hashcode of this instance.
*
* @return {@link Long#hashCode(long)} called on the {@link #id}.
*/
@Override
public int hashCode()
{
return id == null ? 0 : Long.hashCode(id);
}
/**
* Replacement for the default object writing method. The latest state is written to the output
* destination.
*
* @param out
* The output destination to which fields will be saved.
*
* @throws IOException
* In case of I/O errors.
*/
@Override
public void writeExternal(ObjectOutput out)
throws IOException
{
writeLong(out, id);
}
/**
* Replacement for the default object reading method. The latest state is read from the input
* source.
*
* @param in
* The input source from which fields will be restored.
*
* @throws IOException
* In case of I/O errors.
* @throws ClassNotFoundException
* If payload can't be instantiated.
*/
@Override
public void readExternal(ObjectInput in)
throws IOException,
ClassNotFoundException
{
id = readLong(in);
}
}