568 lines
19 KiB
Java

/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.dynamo.unistore;
import java.security.acl.AclNotFoundException;
import java.util.*;
import org.apache.commons.collections.*;
import org.apache.log4j.Logger;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.db.NamespaceCache;
import com.silverwrist.dynamo.db.UserManagement;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.security.SecurityReferenceMonitor;
import com.silverwrist.dynamo.util.*;
class MessageImpl implements UniStoreMessage
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static Logger logger = Logger.getLogger(MessageImpl.class);
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private MessageOps m_ops; // database operations object
private NamespaceCache m_nscache; // namespace cache
private SecurityReferenceMonitor m_srm; // security reference monitor
private UserManagement m_users; // user manager
private long m_id; // the message ID
private long m_parentid; // the parent message ID
private int m_seq; // sequence within parent
private int m_creator; // UID of creator
private java.util.Date m_posted; // date message was posted
private int m_aclid = -1; // ACL id
private ReferenceMap m_properties; // properties cache
private int m_text_count = -1; // number of text parts
private int m_binary_count = -1; // number of binary parts
private ReferenceMap m_part_to_text; // mapping from part index to text part
private ReferenceMap m_pk_to_text; // mapping from property key to text part
private ReferenceMap m_part_to_binary; // mapping from part index to binary part
private ReferenceMap m_pk_to_binary; // mapping from property key to binary part
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
MessageImpl(MessageOps ops, NamespaceCache nscache, SecurityReferenceMonitor srm, UserManagement users, Map params)
{
m_ops = ops;
m_nscache = nscache;
m_srm = srm;
m_users = users;
m_id = ((Long)(params.get(ManagerOps.PARAM_MSGID))).longValue();
m_parentid = ((Long)(params.get(ManagerOps.PARAM_PARENT))).longValue();
m_seq = ((Integer)(params.get(ManagerOps.PARAM_SEQ))).intValue();
m_creator = ((Integer)(params.get(ManagerOps.PARAM_CREATOR))).intValue();
m_posted = (java.util.Date)(params.get(ManagerOps.PARAM_POSTED));
Integer tmp = (Integer)(params.get(ManagerOps.PARAM_ACLID));
if (tmp!=null)
m_aclid = tmp.intValue();
m_properties = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.SOFT);
m_part_to_text = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.SOFT);
m_pk_to_text = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.SOFT);
m_part_to_binary = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.SOFT);
m_pk_to_binary = new ReferenceMap(ReferenceMap.HARD,ReferenceMap.SOFT);
} // end constructor
/*--------------------------------------------------------------------------------
* Implementations from interface ObjectProvider
*--------------------------------------------------------------------------------
*/
/**
* Retrieves an object from this <CODE>ObjectProvider</CODE>.
*
* @param namespace The namespace to interpret the name relative to.
* @param name The name of the object to be retrieved.
* @return The object reference specified.
*/
public Object getObject(String namespace, String name)
{
try
{ // convert the namespace name to an ID here
PropertyKey key = new PropertyKey(m_nscache.namespaceNameToId(namespace),name);
Object rc = null;
synchronized (this)
{ // start by looking in the properties map
rc = m_properties.get(key);
if (rc==null)
{ // no use - need to try the database
rc = m_ops.getProperty(m_id,key);
if (rc!=null)
m_properties.put(key,rc);
} // end if
} // end synchronized block
if (rc==null)
throw new NoSuchObjectException(this.toString(),namespace,name);
return rc;
} // end try
catch (DatabaseException e)
{ // translate into our NoSuchObjectException but retain the DatabaseException
throw new NoSuchObjectException(this.toString(),namespace,name,e);
} // end catch
} // end getObject
/*--------------------------------------------------------------------------------
* Implementations from interface SecureObjectStore
*--------------------------------------------------------------------------------
*/
/**
* Sets an object into this message's properties.
*
* @param caller The user performing the operation.
* @param namespace The namespace to interpret the name relative to.
* @param name The name of the object to be set.
* @param value The object to set into the message's properties.
* @return The previous object that was set into the message's properties under this namespace and name, or
* <CODE>null</CODE> if there was no such object.
* @exception com.silverwrist.dynamo.except.DatabaseException If there was an error setting the object value.
* @exception com.silverwrist.dynamo.except.DynamoSecurityException If the specified user is not permitted to
* set this object value into this message's properties.
*/
public Object setObject(DynamoUser caller, String namespace, String name, Object value)
throws DatabaseException, DynamoSecurityException
{
testPermission(caller,namespace,"set.property","no.setProperty");
Object rc = null;
// convert the namespace name to an ID here
PropertyKey key = new PropertyKey(m_nscache.namespaceNameToId(namespace),name);
synchronized (this)
{ // start by setting the database value
rc = m_ops.setProperty(m_id,key,value);
// and cache it, too
m_properties.put(key,value);
} // end synchronized block
// TODO: m_post.postUpdate(new GlobalPropertyUpdateEvent(this,namespace,name));
return rc;
} // end setObject
/**
* Removes an object from this message's properties.
*
* @param caller The user performing the operation.
* @param namespace The namespace to interpret the name relative to.
* @param name The name of the object to be removed.
* @return The previous object that was set into the message's properties under this namespace and name, or
* <CODE>null</CODE> if there was no such object.
* @exception com.silverwrist.dynamo.except.DatabaseException If there was an error removing the object value.
* @exception com.silverwrist.dynamo.except.DynamoSecurityException If the specified user is not permitted to
* remove this object value from this message's properties.
*/
public Object removeObject(DynamoUser caller, String namespace, String name)
throws DatabaseException, DynamoSecurityException
{
testPermission(caller,namespace,"remove.property","no.removeProperty");
Object rc = null;
// convert the namespace name to an ID here
PropertyKey key = new PropertyKey(m_nscache.namespaceNameToId(namespace),name);
synchronized (this)
{ // start by killing the database value
rc = m_ops.removeProperty(m_id,key);
// and remove the cached value, too
m_properties.remove(key);
} // end synchronized block
// TODO: m_post.postUpdate(new GlobalPropertyUpdateEvent(this,namespace,name));
return rc;
} // end removeObject
/**
* Returns a collection of all object namespaces that have been set into this message's properties.
*
* @return A {@link java.util.Collection Collection} containing {@link java.lang.String String} objects specifying
* all the object namespaces.
* @exception com.silverwrist.dynamo.except.DatabaseException If there was an error getting the namespace list.
*/
public Collection getNamespaces() throws DatabaseException
{
// call through to the database to get the list of namespace IDs
int[] ids = m_ops.getPropertyNamespaceIDs(m_id);
ArrayList rc = new ArrayList(ids.length);
for (int i=0; i<ids.length; i++)
rc.add(m_nscache.namespaceIdToName(ids[i]));
return Collections.unmodifiableList(rc);
} // end getNamespaces
/**
* Returns a collection of all object names that have been set into this message's properties under
* a given namespace.
*
* @param namespace The namespace to look for names under.
* @return A {@link java.util.Collection Collection} containing {@link java.lang.String String} objects
* specifying all the object names for this namespace.
* @exception com.silverwrist.dynamo.except.DatabaseException If there was an error getting the object name list.
*/
public Collection getNamesForNamespace(String namespace) throws DatabaseException
{
// call through to the database to get the data for this namespace
int nsid = m_nscache.namespaceNameToId(namespace);
Map data = m_ops.getAllProperties(m_id,nsid);
// we both create the return value and cache the data values
ArrayList rc = new ArrayList(data.size());
synchronized (this)
{ // do the transfer...
Iterator it = data.entrySet().iterator();
while (it.hasNext())
{ // copy one entry at a time
Map.Entry ntry = (Map.Entry)(it.next());
rc.add(ntry.getKey().toString());
m_properties.put(new PropertyKey(nsid,ntry.getKey().toString()),ntry.getValue());
} // end while
} // end synchronized block
return Collections.unmodifiableList(rc);
} // end getNamesForNamespace
/*--------------------------------------------------------------------------------
* Implementations from interface UniStoreMessage
*--------------------------------------------------------------------------------
*/
public long getMessageID()
{
return m_id;
} // end getMessageID
public long getParentMessageID()
{
return m_parentid;
} // end getParentMessageID
public void setParentMessageID(DynamoUser caller, long id) throws DatabaseException, DynamoSecurityException
{
testPermission(caller,Namespaces.UNISTORE_PERMISSIONS_NAMESPACE,"set.parent","no.setParent");
synchronized (this)
{ // reset the parent message ID
m_ops.setParentMessageID(m_id,id);
m_parentid = id;
} // end synchronized block
// TODO: send out an update?
} // end setParentMessageID
public int getSequence()
{
return m_seq;
} // end getSequence
public void setSequence(DynamoUser caller, int seq) throws DatabaseException, DynamoSecurityException
{
testPermission(caller,Namespaces.UNISTORE_PERMISSIONS_NAMESPACE,"set.parent","no.setParent");
synchronized (this)
{ // reset the sequence number
m_ops.setSequence(m_id,seq);
m_seq = seq;
} // end if
// TODO: send out an update?
} // end setSequence
public int getCreatorUID()
{
return m_creator;
} // end getCreatorUID
public DynamoUser getCreator() throws DatabaseException
{
return m_users.getUser(m_creator);
} // end getCreator
public java.util.Date getPostDate()
{
return m_posted;
} // end getPostDate
public DynamoAcl getAcl() throws DatabaseException, AclNotFoundException
{
return m_srm.getAcl(m_aclid);
} // end getAcl
public void setAcl(DynamoUser caller, DynamoAcl acl) throws DatabaseException, DynamoSecurityException
{
testPermission(caller,Namespaces.UNISTORE_PERMISSIONS_NAMESPACE,"set.ACL","no.setACL");
int aclid = -1;
if (acl!=null)
aclid = acl.getAclID();
synchronized (this)
{ // reset the ACL ID
m_ops.setAclID(m_id,aclid);
m_aclid = aclid;
} // end synchronized block
// TODO: post an update?
} // end setAcl
public synchronized int getNumTextParts() throws DatabaseException
{
if (m_text_count<0)
m_text_count = m_ops.getNumTextParts(m_id);
return m_text_count;
} // end getNumTextParts
public synchronized int getNumBinaryParts() throws DatabaseException
{
if (m_binary_count<0)
m_binary_count = m_ops.getNumBinaryParts(m_id);
return m_binary_count;
} // end getNumBinaryParts
public UniStoreTextPart getTextPart(int index) throws DatabaseException
{
Integer key = new Integer(index);
TextPartImpl rc = null;
synchronized (this)
{ // look in the part-to-text map first
rc = (TextPartImpl)(m_part_to_text.get(key));
if (rc==null)
{ // OK, look up the part in the database
Map params = m_ops.loadTextPart(m_id,index);
PropertyKey otherkey = (PropertyKey)(params.get(MessageOps.PARAM_IDENTITY));
rc = (TextPartImpl)(m_pk_to_text.get(otherkey));
if (rc==null)
{ // need to create a new object
rc = new TextPartImpl(m_ops.getTextPartOps(),m_nscache,this,params);
m_part_to_text.put(key,rc);
m_pk_to_text.put(otherkey,rc);
} // end if
else // feed back into the parts map
m_part_to_text.put(key,rc);
} // end if
else
{ // need to reinsert into property key map
QualifiedNameKey qnk = rc.getPartIdentity();
PropertyKey pk = new PropertyKey(m_nscache.namespaceNameToId(qnk.getNamespace()),qnk.getName());
m_pk_to_text.put(pk,rc);
} // end else
} // end synchronized block
return rc;
} // end getTextPart
public UniStoreTextPart getTextPart(String namespace, String name) throws DatabaseException
{
PropertyKey key = new PropertyKey(m_nscache.namespaceNameToId(namespace),name);
TextPartImpl rc = null;
synchronized (this)
{ // look in the PK-to-text map first
rc = (TextPartImpl)(m_pk_to_text.get(key));
if (rc==null)
{ // OK, look up the part in the database
Map params = m_ops.loadTextPart(m_id,key);
Integer otherkey = (Integer)(params.get(MessageOps.PARAM_PART));
rc = (TextPartImpl)(m_part_to_text.get(otherkey));
if (rc==null)
{ // OK, need to create the object
rc = new TextPartImpl(m_ops.getTextPartOps(),m_nscache,this,params);
m_part_to_text.put(otherkey,rc);
m_pk_to_text.put(key,rc);
} // end if
else // feed back into the PKs map
m_pk_to_text.put(key,rc);
} // end if
else // reinsert into part key map
m_part_to_text.put(new Integer(rc.getPartIndex()),rc);
} // end synchronized block
return rc;
} // end getTextPart
public UniStoreBinaryPart getBinaryPart(int index) throws DatabaseException
{
Integer key = new Integer(index);
BinaryPartImpl rc = null;
synchronized (this)
{ // look in the part-to-binary map first
rc = (BinaryPartImpl)(m_part_to_binary.get(key));
if (rc==null)
{ // OK, look up the part in the database
Map params = m_ops.loadBinaryPart(m_id,index);
PropertyKey otherkey = (PropertyKey)(params.get(MessageOps.PARAM_IDENTITY));
rc = (BinaryPartImpl)(m_pk_to_binary.get(otherkey));
if (rc==null)
{ // need to create a new object
rc = new BinaryPartImpl(m_ops.getBinaryPartOps(),m_nscache,this,params);
m_part_to_binary.put(key,rc);
m_pk_to_binary.put(otherkey,rc);
} // end if
else // feed back into the parts map
m_part_to_binary.put(key,rc);
} // end if
else
{ // need to reinsert into property key map
QualifiedNameKey qnk = rc.getPartIdentity();
PropertyKey pk = new PropertyKey(m_nscache.namespaceNameToId(qnk.getNamespace()),qnk.getName());
m_pk_to_binary.put(pk,rc);
} // end else
} // end synchronized block
return rc;
} // end getBinaryPart
public UniStoreBinaryPart getBinaryPart(String namespace, String name) throws DatabaseException
{
PropertyKey key = new PropertyKey(m_nscache.namespaceNameToId(namespace),name);
BinaryPartImpl rc = null;
synchronized (this)
{ // look in the PK-to-binary map first
rc = (BinaryPartImpl)(m_pk_to_binary.get(key));
if (rc==null)
{ // OK, look up the part in the database
Map params = m_ops.loadBinaryPart(m_id,key);
Integer otherkey = (Integer)(params.get(MessageOps.PARAM_PART));
rc = (BinaryPartImpl)(m_part_to_binary.get(otherkey));
if (rc==null)
{ // OK, need to create the object
rc = new BinaryPartImpl(m_ops.getBinaryPartOps(),m_nscache,this,params);
m_part_to_binary.put(otherkey,rc);
m_pk_to_binary.put(key,rc);
} // end if
else // feed back into the PKs map
m_pk_to_binary.put(key,rc);
} // end if
else // reinsert into part key map
m_part_to_binary.put(new Integer(rc.getPartIndex()),rc);
} // end synchronized block
return rc;
} // end getBinaryPart
public List getTextParts() throws DatabaseException
{
int n = this.getNumTextParts();
ArrayList rc = new ArrayList(n);
for (int i=1; i<=n; i++)
rc.add(this.getTextPart(i));
return Collections.unmodifiableList(rc);
} // end getTextParts
public List getBinaryParts() throws DatabaseException
{
int n = this.getNumBinaryParts();
ArrayList rc = new ArrayList(n);
for (int i=1; i<=n; i++)
rc.add(this.getBinaryPart(i));
return Collections.unmodifiableList(rc);
} // end getBinaryParts
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
void testPermission(DynamoUser caller, String perm_namespace, String perm_name, String fail_message)
throws DatabaseException, DynamoSecurityException
{
if (caller.equals(m_srm.getAdminUser()))
return; // Administrator can do anything
if (m_aclid==-1)
{ // no ACL - rely on fallback method
if (caller.getUID()==m_creator)
return; // the creator can do anything, but no one else can
} // end if
else
{ // test against the ACL
try
{ // look up the ACL
if (m_srm.getAcl(m_aclid).testPermission(caller,perm_namespace,perm_name))
return;
} // end try
catch (AclNotFoundException e)
{ // ACL not found - go to fallback mechanism
logger.warn("ACL " + m_aclid + " not found while testing message " + m_id);
if (caller.getUID()==m_creator)
return; // the creator can do anything, but no one else can
} // end catch
} // end else
// Throw a DynamoSecurityException indicating what you're not permitted to do.
DynamoSecurityException de = new DynamoSecurityException(MessageImpl.class,"UniStoreMessages",fail_message);
de.setParameter(0,String.valueOf(m_id));
throw de;
} // end testPermission
} // end class MessageImpl