/*
* $Id: AnyTribe.java,v 1.19 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.net;
import anvil.Log;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyTuple;
import anvil.core.AnyBindingEnumeration;
import anvil.core.runtime.AnyPermission;
import anvil.script.Context;
import anvil.server.Tribe;
import anvil.server.Citizen;
import anvil.server.Tribe;
import anvil.server.Citizen;
import anvil.server.OperationFailedException;
import anvil.java.util.BindingEnumeration;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.Enumeration;
import java.util.ArrayList;
///
/// @class Tribe
///
/**
* class AnyTribe
*/
public class AnyTribe extends AnyAbstractClass
{
public static anvil.core.RuntimePermission CAN_READ = new anvil.core.RuntimePermission("anvil.net.Tribe", false);
public static anvil.core.RuntimePermission CAN_WRITE = new anvil.core.RuntimePermission("anvil.net.Tribe", true);
private Tribe _tribe;
public AnyTribe(Tribe tribe)
{
_tribe = tribe;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public int hashCode()
{
return _tribe.hashCode();
}
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj instanceof AnyTribe) {
return _tribe.equals(((AnyTribe)obj)._tribe);
}
return false;
}
public Object toObject()
{
return _tribe;
}
public Any getAttribute(anvil.script.Context context, String attribute)
{
Any value = _tribe.getVariable(attribute);
if (value != null) {
return value;
} else {
return Any.UNDEFINED;
}
}
public Any checkAttribute(anvil.script.Context context, String attribute)
{
return getAttribute(context, attribute);
}
public Any setAttribute(anvil.script.Context context, String attribute, Any value)
{
context.checkAccess(CAN_WRITE);
return _tribe.setVariable(attribute, value);
}
public boolean deleteAttribute(anvil.script.Context context, String attribute)
{
context.checkAccess(CAN_WRITE);
return _tribe.deleteVariable(attribute);
}
public Any getReference(anvil.script.Context context, Any index)
{
return getAttribute(context, index.toString());
}
public Any checkReference(anvil.script.Context context, Any index)
{
return checkAttribute(context, index.toString());
}
public Any setReference(anvil.script.Context context, Any index, Any value)
{
return setAttribute(context, index.toString(), value);
}
public boolean deleteReference(anvil.script.Context context, Any index)
{
return deleteAttribute(context, index.toString());
}
public BindingEnumeration enumeration()
{
return _tribe.getVariables();
}
/// @method getName
/// Returns the name of this tribe.
/// @synopsis string getName()
public Any m_getName()
{
return Any.create(_tribe.getName());
}
/// @method commit
/// Commits the changes made to this tribe.
/// @synopsis boolean commit()
/// @throws OperationFailed If operation failed
/// @throws AccessDenied If security policy denies this operation
public Any m_commit(Context context)
{
context.checkAccess(CAN_WRITE);
try {
_tribe.commit();
return this;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method getParents
/// Returns a list of parent tribes.
/// @synopsis list getParents()
public Any m_getParents()
{
Tribe[] tribes = _tribe.getParents();
int n = tribes.length;
Any[] list = new Any[n];
for(int i=0; i<n; i++) {
list[i] = new AnyTribe(tribes[i]);
}
return new AnyTuple(list);
}
/// @method hasChilds
/// Checks if this tribe has children tribes.
/// @synopsis boolean hasChilds()
public Any m_hasChilds()
{
return _tribe.hasChilds() ? TRUE : FALSE;
}
/// @method getChilds
/// Returns a list of children tribes.
/// @synopsis list getChilds()
public Any m_getChilds()
{
Tribe[] tribes = _tribe.getChilds();
int n = tribes.length;
Any[] list = new Any[n];
for(int i=0; i<n; i++) {
list[i] = new AnyTribe(tribes[i]);
}
return new AnyTuple(list);
}
/// @method getCitizens
/// Returns a list of citizens belonging to this tribe.
/// @synopsis list getCitizens()
/// @throws AccessDenied If security policy denies this operation
public Any m_getCitizens(Context context)
{
context.checkAccess(AnyCitizen.CAN_READ);
Citizen[] citizens = _tribe.getCitizens();
int n = citizens.length;
Any[] list = new Any[n];
for(int i=0; i<n; i++) {
list[i] = new AnyCitizen(citizens[i]);
}
return new AnyTuple(list);
}
/// @method getIdentity
/// Returns identity (memoery address) for this tribe.
/// @synopsis int getIdentity()
public Any m_getIdentity()
{
return Any.create(System.identityHashCode(_tribe));
}
public static Tribe find(Tribe t, int h)
{
if (t == null) {
return null;
}
if (System.identityHashCode(t) == h) {
return t;
}
Tribe[] c = t.getChilds();
int n = c.length;
for(int i=0; i<n; i++) {
t = find(c[i], h);
if (t != null) {
return t;
}
}
return null;
}
/// @method find
/// Recursively searches for tribe with given identity.
/// @synopsis Tribe find(int identity)
public static final Object[] p_find = { "identity" };
public Any m_find(int identity)
{
Tribe t = find(_tribe, identity);
return (t != null) ? new AnyTribe(t) : UNDEFINED;
}
/// @method attach
/// Attaches given citizens and tribes into this tribe.
/// @synopsis void attach(tribe, ..)
/// @synopsis void attach(citizen, ..)
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_attach = { null, "tribesOrCitizens" };
public Any m_attach(Context context, Any[] parameters)
{
context.checkAccess(CAN_WRITE);
context.checkAccess(AnyCitizen.CAN_WRITE);
int n = parameters.length;
if (n < 1) {
throw parametersMissing(context, "attach");
}
for(int i=0; i<n; i++) {
Any p = parameters[i];
try {
if (p instanceof AnyTribe) {
_tribe.attach((Tribe)p.toObject());
} else if (p instanceof AnyCitizen) {
_tribe.attach((Citizen)p.toObject());
} else {
throw context.BadParameter("Tribe or citizen expected");
}
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
return this;
}
/// @method detach
/// Detaches citizens or tribes from this tribe.
/// @synopsis void detach(tribe, ..)
/// @synopsis void detach(citizen, ..)
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_detach = { null, "tribesOrCitizens" };
public Any m_detach(Context context, Any[] parameters)
{
context.checkAccess(CAN_WRITE);
context.checkAccess(AnyCitizen.CAN_WRITE);
int n = parameters.length;
if (n < 1) {
throw parametersMissing(context, "detach");
}
for(int i=0; i<n; i++) {
Any p = parameters[i];
try {
if (p instanceof AnyTribe) {
_tribe.detach((Tribe)p.toObject());
} else if (p instanceof AnyCitizen) {
_tribe.detach((Citizen)p.toObject());
} else {
throw context.BadParameter("Tribe or citizen expected");
}
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
return this;
}
/// @method addPermission
/// Adds permission to tribe's security policy. You may have to call
/// commit() for the tribe to save modification to persistent store
/// (depenging on realm implementation)
/// @synopsis void addPermission(permission)
/// @throws OperationFailed If error occured file saving to persistent
public static final Object[] p_addPermission = { null, "permission" };
public Any m_addPermission(Context context, Any permission)
{
context.checkAccess(CAN_WRITE);
if (!(permission instanceof AnyPermission)) {
throw context.BadParameter("Permission expected");
}
try {
_tribe.addPermission( (Permission)permission.toObject() );
} catch (OperationFailedException e) {
throw context.exception(e);
}
return this;
}
/// @method removePermission
/// Removes permission from tribe's security policy. You may have to call
/// commit() for the tribe to save modification to persistent store
/// (depenging on realm implementation)
/// @synopsis void removePermission(permission)
/// @throws OperationFailed If error occured file removing from persistent
public static final Object[] p_removePermission = { null, "permission" };
public Any m_removePermission(Context context, Any permission)
{
context.checkAccess(CAN_WRITE);
if (!(permission instanceof AnyPermission)) {
throw context.BadParameter("Permission expected");
}
try {
_tribe.removePermission( (Permission)permission.toObject() );
} catch (OperationFailedException e) {
throw context.exception(e);
}
return this;
}
/// @method getPermissions
/// Returns permissions that this tribe has.
/// @synopsis enumeration getPermissions()
/// @throws AccessDenied If security policy denies this operation
public Any m_getPermissions(Context context)
{
context.checkAccess(AnyRealm.CAN_READ);
PermissionCollection pers = _tribe.getPermissions();
Enumeration enu = pers.elements();
ArrayList list = new ArrayList();
while (enu.hasMoreElements()) {
list.add( new AnyPermission((Permission)enu.nextElement()) );
}
return new AnyTuple( (Any[])list.toArray(new Any[list.size()]) );
}
/// @method getCombinedPermissions
/// Returns permissions that this tribe has (including permissions inherited from groups).
/// @synopsis enumeration getCombinedPermissions()
/// @throws AccessDenied If security policy denies this operation
public Any m_getCombinedPermissions(Context context)
{
context.checkAccess(AnyRealm.CAN_READ);
Enumeration enu = _tribe.getCombinedPermissions().elements();
ArrayList list = new ArrayList();
while (enu.hasMoreElements()) {
list.add( new AnyPermission((Permission)enu.nextElement()) );
}
return new AnyTuple( (Any[])list.toArray(new Any[list.size()]) );
}
/// @method remove
/// Removes this tribe.
/// @synopsis void remove()
/// @throws AccessDenied If security policy denies this operation
public Any m_remove(Context context)
{
context.checkAccess(CAN_WRITE);
try {
_tribe.remove();
return this;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method getRealm
/// Returns the realm into which this tribe belongs to.
/// @synopsis Realm getRealm()
/// @throws AccessDenied If security policy denies this operation
public Any m_getRealm(Context context)
{
context.checkAccess(AnyRealm.CAN_READ);
return new AnyRealm(_tribe.getRealm());
}
/// @method listPermissions
/// Returns enumeration of permissions held by this tribe.
/// @synopsis enumeration listPermissions()
public Any m_listPermissions()
{
return new AnyBindingEnumeration(_tribe.listPermissions());
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Tribe", AnyTribe.class,
//DOC{{
""+
"\n" +
" @class Tribe\n" +
"\n" +
" @method getName\n" +
" Returns the name of this tribe.\n" +
" @synopsis string getName()\n" +
" @method commit\n" +
" Commits the changes made to this tribe.\n" +
" @synopsis boolean commit()\n" +
" @throws OperationFailed If operation failed\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method getParents\n" +
" Returns a list of parent tribes.\n" +
" @synopsis list getParents()\n" +
" @method hasChilds\n" +
" Checks if this tribe has children tribes.\n" +
" @synopsis boolean hasChilds()\n" +
" @method getChilds\n" +
" Returns a list of children tribes.\n" +
" @synopsis list getChilds()\n" +
" @method getCitizens\n" +
" Returns a list of citizens belonging to this tribe.\n" +
" @synopsis list getCitizens()\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method getIdentity\n" +
" Returns identity (memoery address) for this tribe.\n" +
" @synopsis int getIdentity()\n" +
" @method find\n" +
" Recursively searches for tribe with given identity.\n" +
" @synopsis Tribe find(int identity)\n" +
" @method attach\n" +
" Attaches given citizens and tribes into this tribe.\n" +
" @synopsis void attach(tribe, ..)\n" +
" @synopsis void attach(citizen, ..)\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method detach\n" +
" Detaches citizens or tribes from this tribe.\n" +
" @synopsis void detach(tribe, ..)\n" +
" @synopsis void detach(citizen, ..)\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method addPermission\n" +
" Adds permission to tribe's security policy. You may have to call\n" +
" commit() for the tribe to save modification to persistent store\n" +
" (depenging on realm implementation)\n" +
" @synopsis void addPermission(permission)\n" +
" @throws OperationFailed If error occured file saving to persistent\n" +
" @method removePermission\n" +
" Removes permission from tribe's security policy. You may have to call\n" +
" commit() for the tribe to save modification to persistent store\n" +
" (depenging on realm implementation)\n" +
" @synopsis void removePermission(permission)\n" +
" @throws OperationFailed If error occured file removing from persistent\n" +
" @method getPermissions\n" +
" Returns permissions that this tribe has.\n" +
" @synopsis enumeration getPermissions()\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method getCombinedPermissions\n" +
" Returns permissions that this tribe has (including permissions inherited from groups).\n" +
" @synopsis enumeration getCombinedPermissions()\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method remove\n" +
" Removes this tribe.\n" +
" @synopsis void remove()\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method getRealm\n" +
" Returns the realm into which this tribe belongs to.\n" +
" @synopsis Realm getRealm()\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method listPermissions\n" +
" Returns enumeration of permissions held by this tribe.\n" +
" @synopsis enumeration listPermissions()\n"
//}}DOC
);
static {
NetModule.class.getName();
}
}