Skip to content
Permalink
Browse files
Search TypeDescriptor cache first in meta(repId)
  • Loading branch information
ngmr committed May 21, 2015
1 parent cc3d87d commit 2db00841e77277a1196a16b564a25e85502b7fbd
Showing 8 changed files with 157 additions and 574 deletions.
@@ -39,6 +39,13 @@ public Class getJavaClass() {
return _java_class;
}

@Override
public String toString() {
return String.format("%s{class=\"%s\",repId=\"%s\",arrRepId=\"%s\"}",
this.getClass().getName(), getJavaClass(),
getRepositoryID(), getRepositoryIDForArray());
}

protected TypeDescriptor(Class type, TypeRepository repository) {
_java_class = type;
String typeName = type.getName();
@@ -20,16 +20,18 @@

import java.io.Externalizable;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.sql.Date;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
@@ -45,31 +47,41 @@
import org.omg.SendingContext.CodeBase;
import org.omg.SendingContext.CodeBaseHelper;
import org.omg.SendingContext.RunTime;

import org.apache.yoko.rmi.util.SearchKey;
import org.apache.yoko.rmi.util.WeakKey;

public class TypeRepository {
static final Logger logger = Logger.getLogger(TypeRepository.class
.getName());
static final Logger logger = Logger.getLogger(TypeRepository.class.getName());

org.omg.CORBA.ORB orb;

private static final class RepIdWeakMap {
private final Map<String, WeakReference<TypeDescriptor>> map =
Collections.synchronizedMap(new WeakHashMap<String,WeakReference<TypeDescriptor>>());
private static final class TypeDescriptorCache {
private final ConcurrentMap<WeakKey<String>, WeakReference<TypeDescriptor>> map = new ConcurrentHashMap<>();
private final ReferenceQueue<String> staleKeys = new ReferenceQueue<>();

public TypeDescriptor get(String repId) {
cleanStaleKeys();
WeakReference<TypeDescriptor> ref = map.get(new SearchKey<String>(repId));
return (null == ref) ? null : ref.get();
}

void put(String repId, TypeDescriptor desc) {
map.put(repId, new WeakReference<TypeDescriptor>(desc));
public void put(TypeDescriptor typeDesc) {
cleanStaleKeys();
final WeakReference<TypeDescriptor> value = new WeakReference<>(typeDesc);
map.putIfAbsent(new WeakKey<String>(typeDesc.getRepositoryID(), staleKeys), value);
map.putIfAbsent(new WeakKey<String>(typeDesc.getRepositoryIDForArray(), staleKeys), value);
}

TypeDescriptor get(String repId) {
WeakReference<TypeDescriptor> value = map.get(repId);
return (value == null) ? null : value.get();
private void cleanStaleKeys() {
for (Reference<? extends String> staleKey = staleKeys.poll(); staleKey != null; staleKey = staleKeys.poll()) {
map.remove(staleKey);
}
}
}

private static final class LocalDescriptors extends ClassValue<TypeDescriptor> {
private static final class Raw extends ClassValue<TypeDescriptor> {
private static final List<Class<?>> staticAnyTypes =
private static final List<Class<?>> staticAnyTypes =
Collections.unmodifiableList(
Arrays.asList(Object.class, Externalizable.class, Serializable.class, Remote.class));

@@ -178,17 +190,17 @@ private static boolean isRemoteMethod(java.lang.reflect.Method m) {
}

private final Raw rawValues;
private final RepIdWeakMap repIdDescriptors;
private final TypeDescriptorCache repIdDescriptors;

LocalDescriptors(TypeRepository repo, RepIdWeakMap repIdDescriptors) {
LocalDescriptors(TypeRepository repo, TypeDescriptorCache repIdDescriptors) {
rawValues = new Raw(repo);
this.repIdDescriptors = repIdDescriptors;
}
@Override
protected TypeDescriptor computeValue(Class<?> type) {
final TypeDescriptor desc = rawValues.get(type);
desc.init();
repIdDescriptors.put(desc.getRepositoryID(), desc);
repIdDescriptors.put(desc);
return desc;
}

@@ -199,24 +211,31 @@ private static final class FvdRepIdDescriptorMaps extends ClassValue<ConcurrentM
@Override
protected ConcurrentMap<String,ValueDescriptor> computeValue(
Class<?> type) {
return new ConcurrentHashMap<String,ValueDescriptor>();
return new ConcurrentHashMap<String,ValueDescriptor>(1);
}
}

private final RepIdWeakMap repIdDescriptors;
private final TypeDescriptorCache repIdDescriptors;
private final LocalDescriptors localDescriptors;
private final FvdRepIdDescriptorMaps fvdDescMaps = new FvdRepIdDescriptorMaps();
private final ConcurrentMap<String,ValueDescriptor> noTypeDescMap = new ConcurrentHashMap<String,ValueDescriptor>();

private static final Set<Class<?>> initTypes;

static {
initTypes = createClassSet(Object.class, String.class, ClassDesc.class, Date.class,
Externalizable.class, Serializable.class, Remote.class);
}

private static Set<Class<?>> createClassSet(Class<?>...types) {
return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(types)));
}

public TypeRepository(org.omg.CORBA.ORB orb) {
this.orb = orb;
repIdDescriptors = new RepIdWeakMap();
repIdDescriptors = new TypeDescriptorCache();
localDescriptors = new LocalDescriptors(this, repIdDescriptors);

Class<?>[] initTypes = {
Object.class, String.class, ClassDesc.class, Date.class,
Externalizable.class, Serializable.class, Remote.class };

for (Class<?> type: initTypes) {
localDescriptors.get(type);
}
@@ -260,9 +279,20 @@ public RemoteInterfaceDescriptor getRemoteDescriptor(Class<?> type) {
}

public TypeDescriptor getDescriptor(Class<?> type) {
logger.fine("Requesting type descriptor for class " + type.getName());
if (logger.isLoggable(Level.FINE))
logger.fine(String.format("Requesting type descriptor for class \"%s\"", type.getName()));
final TypeDescriptor desc = localDescriptors.get(type);
logger.fine("Class " + type.getName() + " resolves to " + desc.getClass().getName());
if (logger.isLoggable(Level.FINE))
logger.fine(String.format("Class \"%s\" resolves to %s", type.getName(), desc));
return desc;
}

public TypeDescriptor getDescriptor(String repId) {
if (logger.isLoggable(Level.FINE))
logger.fine(String.format("Requesting type descriptor for repId \"%s\"", repId));
final TypeDescriptor desc = repIdDescriptors.get(repId);
if (logger.isLoggable(Level.FINE))
logger.fine(String.format("RepId \"%s\" resolves to %s", repId, desc));
return desc;
}

@@ -320,10 +350,10 @@ public ValueDescriptor getDescriptor(Class<?> clz, String repid,

ValueDescriptor newDesc = new FVDValueDescriptor(fvd, clz, this, repid, super_desc);
ConcurrentMap<String, ValueDescriptor> remoteDescMap = (clz == null) ? noTypeDescMap : fvdDescMaps.get(clz);
clzdesc = remoteDescMap.putIfAbsent(repid, newDesc);
clzdesc = remoteDescMap.putIfAbsent(newDesc.getRepositoryID(), newDesc);
if (clzdesc == null) {
clzdesc = newDesc;
repIdDescriptors.put(repid, clzdesc);
repIdDescriptors.put(clzdesc);
}

return clzdesc;
@@ -409,14 +439,14 @@ static ByteString fixName(ByteString name) {
int len = current.length();
match = false;

for (ByteString reservedPostfixe : reservedPostfixes) {
if (current.endsWith(reservedPostfixe)) {
for (ByteString reservedPostfix : reservedPostfixes) {
if (current.endsWith(reservedPostfix)) {
ByteBuffer buf = new ByteBuffer();
buf.append('_');
buf.append(result);
result = buf.toByteString();

int resultLen = reservedPostfixe.length();
int resultLen = reservedPostfix.length();
if (len > resultLen)
current = current.substring(0, len - resultLen);
else
@@ -432,16 +462,12 @@ static ByteString fixName(ByteString name) {
return name;
}

static final java.util.Set<ByteString> keyWords = new java.util.HashSet<ByteString>();

static final ByteString[] reservedPostfixes = new ByteString[] {
new ByteString("Helper"), new ByteString("Holder"),
new ByteString("Operations"), new ByteString("POA"),
new ByteString("POATie"), new ByteString("Package"),
new ByteString("ValueFactory") };
private static final Set<ByteString> keyWords;
private static final Set<ByteString> reservedPostfixes;

static {
String[] words = { "abstract", "boolean", "break", "byte", "case",
keyWords = createByteStringSet(
"abstract", "boolean", "break", "byte", "case",
"catch", "char", "class", "clone", "const", "continue",
"default", "do", "double", "else", "equals", "extends",
"false", "final", "finalize", "finally", "float", "for",
@@ -451,11 +477,17 @@ static ByteString fixName(ByteString name) {
"protected", "public", "return", "short", "static", "super",
"switch", "synchronized", "this", "throw", "throws",
"toString", "transient", "true", "try", "void", "volatile",
"wait", "while" };
"wait", "while");
reservedPostfixes = createByteStringSet(
"Helper", "Holder", "Operations", "POA", "POATie", "Package", "ValueFactory");
}

private static Set<ByteString> createByteStringSet(String...words) {
final Set<ByteString> set = new HashSet<>(words.length);
for (String word : words) {
keyWords.add(new ByteString(word));
set.add(new ByteString(word));
}
return Collections.unmodifiableSet(set);
}

}
@@ -50,6 +50,10 @@ private ValueDescriptor desc(Class clz) {
return (ValueDescriptor) getRepository().getDescriptor(clz);
}

private ValueDescriptor desc(String repId) {
return (ValueDescriptor)getRepository().getDescriptor(repId);
}

private ValueDescriptor desc(Class clz, String repid, RunTime runtime) {
try {
return (ValueDescriptor) getRepository().getDescriptor(clz, repid,
@@ -263,25 +267,23 @@ String[] getImplementations(String[] ids) {
return result;
}

FullValueDescription meta(String id) {
FullValueDescription meta(String repId) {
if (logger.isLoggable(Level.FINER))
logger.finer(String.format("meta \"%s\"", repId));
try {
// System.out.println ("ValueHandlerImpl::meta "+id);

Class clz = getClassFromRepositoryID(id);

if (clz == null) {
logger.warning("class not found: " + id);
throw new org.omg.CORBA.MARSHAL(0x4f4d0001,
CompletionStatus.COMPLETED_MAYBE);
}
ValueDescriptor desc = desc(repId);
if (null == desc) {
Class clz = getClassFromRepositoryID(repId);

if (clz == null) {
logger.warning("class not found: " + repId);
throw new org.omg.CORBA.MARSHAL(0x4f4d0001,
CompletionStatus.COMPLETED_MAYBE);
}

if (logger.isLoggable(Level.FINER)) {
logger.finer("meta " + id);
desc = desc(clz);
}

ValueDescriptor desc = (ValueDescriptor) getRepository()
.getDescriptor(clz);

return desc.getFullValueDescription();
} catch (Throwable ex) {
logger.log(Level.WARNING, "exception in meta", ex);

0 comments on commit 2db0084

Please sign in to comment.