/*
* $Id: AnyRealm.java,v 1.18 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 java.util.ArrayList;
import anvil.Log;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyTuple;
import anvil.core.AnyList;
import anvil.core.Array;
import anvil.script.Context;
import anvil.server.Citizen;
import anvil.server.Realm;
import anvil.server.Tribe;
import anvil.server.Realm;
import anvil.server.Tribe;
import anvil.server.Citizen;
import anvil.server.OperationFailedException;
import anvil.java.util.BindingEnumeration;
///
/// @class Realm
///
/**
* class AnyRealm
*/
public class AnyRealm extends AnyAbstractClass
{
private Realm _realm;
public static anvil.core.RuntimePermission CAN_READ = new anvil.core.RuntimePermission("anvil.net.Realm", false);
public static anvil.core.RuntimePermission CAN_WRITE = new anvil.core.RuntimePermission("anvil.net.Realm", true);
public AnyRealm(Realm realm)
{
_realm = realm;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public Object toObject()
{
return _realm;
}
public Any getAttribute(anvil.script.Context context, String attribute)
{
context.checkAccess(AnyCitizen.CAN_READ);
Citizen citizen = _realm.getCitizen(attribute);
if (citizen != null) {
return new AnyCitizen(citizen);
} else {
return Any.UNDEFINED;
}
}
public Any checkAttribute(anvil.script.Context context, String attribute)
{
return getAttribute(context, 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());
}
/// @method getCitizen
/// Retrives a citizen with given name.
/// @synopsis Citizen getCitizen(string name)
/// @return Citizen, or undefined
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_getCitizen = new Object[] { null, "name" };
public Any m_getCitizen(Context context, String name)
{
context.checkAccess(AnyCitizen.CAN_READ);
Citizen citizen = _realm.getCitizen(name);
if (citizen != null) {
return new AnyCitizen(citizen);
} else {
return UNDEFINED;
}
}
/// @method getTribe
/// Retrives a tribe with given name.
/// @synopsis Tribe getTribe(string name)
/// @return Tribe, or undefined
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_getTribe = new Object[] { null, "name" };
public Any m_getTribe(Context context, String name)
{
context.checkAccess(AnyTribe.CAN_READ);
Tribe tribe = _realm.getTribe(name);
if (tribe != null) {
return new AnyTribe(tribe);
} else {
return UNDEFINED;
}
}
/// @method createCitizen
/// Creates a news citizen with given name and credentials.
/// Optional key-value mappings may be provided to initialize the citizen
/// with variables.
/// @synopsis Citizen createCitizen(string name, string credentials)
/// @synopsis Citizen createCitizen(string name, string credentials, array mappings)
/// @return Citizen
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_createCitizen = new Object[] { null, "name", "credentials", "*mappings", null };
public Any m_createCitizen(Context context, String name, String credentials, Any mappings)
{
context.checkAccess(CAN_WRITE);
context.checkAccess(AnyCitizen.CAN_WRITE);
try {
Citizen citizen = null;
if (mappings != null) {
BindingEnumeration be = mappings.enumeration();
String[][] params = new String[mappings.sizeOf()][2];
for (int i=0; be.hasMoreElements(); i++) {
params[i][0] = be.nextKey().toString();
params[i][1] = be.nextElement().toString();
}
citizen = _realm.createCitizen(name, credentials, params);
} else {
citizen = _realm.createCitizen(name, credentials);
}
return (citizen != null) ? new AnyCitizen(citizen) : UNDEFINED;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method createTribe
/// Creates a new tribe with given name.
/// @synopsis Tribe createTribe(string name)
/// @return Tribe
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_createTribe = new Object[] { null, "name" };
public Any m_createTribe(Context context, String name)
{
context.checkAccess(CAN_WRITE);
context.checkAccess(AnyTribe.CAN_WRITE);
try {
Tribe tribe = _realm.createTribe(name);
return (tribe != null) ? new AnyTribe(tribe) : UNDEFINED;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method setRoot
/// Sets the root tribe of this realm
/// @synopsis void setRoot(Tribe tribe)
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_setRoot = new Object[] { null, "tribe" };
public Any m_setRoot(Context context, Any tribe)
{
context.checkAccess(CAN_WRITE);
Object o = tribe.toObject();
if (!(o instanceof Tribe)) {
throw context.BadParameter("Tribe expected");
}
try {
_realm.setRoot((Tribe)o);
return this;
} catch (OperationFailedException e) {
throw context.exception(e);
}
}
/// @method getRoot
/// Returns the root tribe of this realm.
/// @synopsis Tribe getRoot()
/// @throws AccessDenied If security policy denies this operation
public Any m_getRoot(Context context)
{
context.checkAccess(AnyTribe.CAN_READ);
Tribe root = _realm.getRoot();
return (root != null) ? new AnyTribe(root) : UNDEFINED;
}
/// @method find
/// Tryins to locate entity with given address from this realm.
/// @synopsis Tribe find(int addressOf)
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_find = new Object[] { null, "address" };
public Any m_find(Context context, int address)
{
context.checkAccess(AnyTribe.CAN_READ);
Tribe t = AnyTribe.find(_realm.getRoot(), address);
return (t != null) ? new AnyTribe(t) : UNDEFINED;
}
/// @method search
/// Searches for citizens with given variable set to given value.
/// @synopsis list search(string name, string value)
/// @return possibly empty list of matching citizens
/// @throws AccessDenied If security policy denies this operation
public static final Object[] p_search = new Object[] { null, "name", "value" };
public Any m_search(Context context, String name, String value)
{
context.checkAccess(AnyCitizen.CAN_READ);
Citizen[] citizen = _realm.searchCitizenByVariable(name, value);
if (citizen != null) {
int n = citizen.length;
Any[] list = new Any[n];
for(int i=0; i<n; i++) {
list[i] = new AnyCitizen(citizen[i]);
}
return new AnyList(list);
} else {
return new AnyList(ARRAY0);
}
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Realm", AnyRealm.class,
//DOC{{
""+
"\n" +
" @class Realm\n" +
"\n" +
" @method getCitizen\n" +
" Retrives a citizen with given name.\n" +
" @synopsis Citizen getCitizen(string name)\n" +
" @return Citizen, or undefined\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method getTribe\n" +
" Retrives a tribe with given name.\n" +
" @synopsis Tribe getTribe(string name)\n" +
" @return Tribe, or undefined\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method createCitizen\n" +
" Creates a news citizen with given name and credentials.\n" +
" Optional key-value mappings may be provided to initialize the citizen\n" +
" with variables.\n" +
" @synopsis Citizen createCitizen(string name, string credentials)\n" +
" @synopsis Citizen createCitizen(string name, string credentials, array mappings)\n" +
" @return Citizen\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method createTribe\n" +
" Creates a new tribe with given name.\n" +
" @synopsis Tribe createTribe(string name)\n" +
" @return Tribe\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method setRoot\n" +
" Sets the root tribe of this realm\n" +
" @synopsis void setRoot(Tribe tribe)\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method getRoot\n" +
" Returns the root tribe of this realm.\n" +
" @synopsis Tribe getRoot()\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method find\n" +
" Tryins to locate entity with given address from this realm.\n" +
" @synopsis Tribe find(int addressOf)\n" +
" @throws AccessDenied If security policy denies this operation\n" +
" @method search\n" +
" Searches for citizens with given variable set to given value.\n" +
" @synopsis list search(string name, string value)\n" +
" @return possibly empty list of matching citizens \n" +
" @throws AccessDenied If security policy denies this operation\n"
//}}DOC
);
static {
NetModule.class.getName();
}
}