001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.appevent;
006:
007: import com.tc.net.protocol.tcm.ChannelIDProvider;
008: import com.tc.object.tx.UnlockedSharedObjectException;
009: import com.tc.object.util.ReadOnlyException;
010:
011: /**
012: * A factory for creating the proper {@link NonPortableEventContext}
013: */
014: public class NonPortableEventContextFactory {
015:
016: private final ChannelIDProvider provider;
017:
018: /**
019: * Construct the factory with the provider of a ChannelID
020: * @param provider The provider
021: */
022: public NonPortableEventContextFactory(ChannelIDProvider provider) {
023: this .provider = provider;
024: }
025:
026: private String getJVMId() {
027: return "VM(" + provider.getChannelID().toLong() + ")";
028: }
029:
030: /**
031: * Create a context for when a non-portable object is found while traversing an object graph
032: * @param pojo The non portable object
033: * @return The context
034: */
035: public NonPortableEventContext createNonPortableEventContext(
036: Object pojo) {
037: return new NonPortableEventContext(pojo, Thread.currentThread()
038: .getName(), getJVMId());
039: }
040:
041: /**
042: * Create a context for when a non-portable object is set as a root
043: * @param rootName The root name
044: * @param rootValue The non-portable value being set
045: * @return The context
046: */
047: public NonPortableRootContext createNonPortableRootContext(
048: String rootName, Object rootValue) {
049: return new NonPortableRootContext(Thread.currentThread()
050: .getName(), getJVMId(), rootName, rootValue);
051: }
052:
053: /**
054: * Create a context for when a non-portable object is set on a field
055: * @param pojo The object that is having the field set
056: * @param fieldName The field of the pojo
057: * @param fieldValue The non-portable value being set
058: * @return The context
059: */
060: public NonPortableFieldSetContext createNonPortableFieldSetContext(
061: Object pojo, String fieldName, Object fieldValue) {
062: return new NonPortableFieldSetContext(Thread.currentThread()
063: .getName(), getJVMId(), pojo, fieldName, fieldValue);
064: }
065:
066: /**
067: * Create a context for an invocation of a logical method with a non-portable object
068: * @param pojo The logical object
069: * @param methodName The method being called on the pojo
070: * @param params The parameters passed to the method
071: * @param index The index into the params of the non-portable object
072: * @return The context
073: */
074: public NonPortableLogicalInvokeContext createNonPortableLogicalInvokeContext(
075: Object pojo, String methodName, Object[] params, int index) {
076: return new NonPortableLogicalInvokeContext(pojo, Thread
077: .currentThread().getName(), getJVMId(), methodName,
078: params, index);
079: }
080:
081: /**
082: * Create a context for when there is an attempt to access a shared object outside the scope of
083: * a shared lock.
084: * @param pojo The object being accessed
085: * @param classname The class of the object
086: * @param fieldname The field of the object being accessed
087: * @param ex The exception thrown
088: * @return The context
089: */
090: public UnlockedSharedObjectEventContext createUnlockedSharedObjectEventContext(
091: Object pojo, String classname, String fieldname,
092: UnlockedSharedObjectException ex) {
093: return new UnlockedSharedObjectEventContext(pojo, classname,
094: fieldname, Thread.currentThread().getName(),
095: getJVMId(), ex);
096: }
097:
098: /**
099: * Create a context for when there is an attempt to access a shared object outside the scope of
100: * a shared lock.
101: * @param pojo The object being accessed
102: * @param ex The exception thrown
103: * @return The context
104: */
105: public UnlockedSharedObjectEventContext createUnlockedSharedObjectEventContext(
106: Object pojo, UnlockedSharedObjectException ex) {
107: return new UnlockedSharedObjectEventContext(pojo, Thread
108: .currentThread().getName(), getJVMId(), ex);
109: }
110:
111: /**
112: * Create a context for when a transaction with read-only access is attempting
113: * to modify a shared object.
114: * @param pojo The object being accessed
115: * @param ex The exception that occurred
116: * @return The context
117: */
118: public ReadOnlyObjectEventContext createReadOnlyObjectEventContext(
119: Object pojo, ReadOnlyException ex) {
120: return new ReadOnlyObjectEventContext(pojo, Thread
121: .currentThread().getName(), getJVMId(), ex);
122: }
123:
124: /**
125: * Create a context for when a transaction with read-only access is attempting
126: * to modify a shared object.
127: * @param pojo The object being accessed
128: * @param classname The class of the pojo
129: * @param fieldname The field name of the object
130: * @param ex The exception that occurred
131: * @return The context
132: */
133: public ReadOnlyObjectEventContext createReadOnlyObjectEventContext(
134: Object pojo, String classname, String fieldname,
135: ReadOnlyException ex) {
136: return new ReadOnlyObjectEventContext(pojo, classname,
137: fieldname, Thread.currentThread().getName(),
138: getJVMId(), ex);
139: }
140: }
|