summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/persistency/ProxyLoader.java
blob: 4324e94754308db8735ef249ffbfe059e35295cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.c2kernel.persistency;
import java.util.HashMap;
import java.util.StringTokenizer;

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.AgentHelper;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.entity.ItemHelper;
import com.c2kernel.entity.ManageableEntity;
import com.c2kernel.lookup.EntityPath;
import com.c2kernel.lookup.LDAPLookup;
import com.c2kernel.persistency.outcome.Outcome;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;

/** Used by proxies to load clusters by queryData from the Entity.
*   Last client storage - only used if not cached elsewhere
*/

public class ProxyLoader extends ClusterStorage {
    HashMap<Integer, ManageableEntity> entities = new HashMap<Integer, ManageableEntity>();
    LDAPLookup lookup;

    @Override
	public void open() throws ClusterStorageException {
        lookup = Gateway.getLDAPLookup();
    }

    @Override
	public void close() throws ClusterStorageException {
    }
    // introspection
    @Override
	public short queryClusterSupport(String clusterType) {
        return READ;
    }

    @Override
	public String getName() {
        return "Proxy Cluster Loader";
    }

    @Override
	public String getId() {
        return "CORBA";
    }

    // retrieve object by path
    @Override
	public C2KLocalObject get(Integer sysKey, String path) throws ClusterStorageException {
        try {
            ManageableEntity thisEntity = getIOR(sysKey);
            String type = getClusterType(path);

            // fetch the xml from the item
            String queryData = thisEntity.queryData(path);
            if (Logger.doLog(6)) Logger.msg(6, "ProxyLoader - "+sysKey+":"+path+" = "+queryData);

            if (queryData != null) {
                if (type.equals(OUTCOME))
                    return new Outcome(path, queryData);
                else
                    return (C2KLocalObject)Gateway.getMarshaller().unmarshall(queryData);
            }
        } catch (ObjectNotFoundException e) {
        	return null;
        } catch (Exception e) {
            Logger.error(e);
            throw new ClusterStorageException(e.getMessage());
        }
        return null;
    }

    // store object by path
    @Override
	public void put(Integer sysKey, C2KLocalObject obj) throws ClusterStorageException {
        // not supported
        throw new ClusterStorageException("Cannot write to items through the ProxyLoader");
    }
    // delete cluster
    @Override
	public void delete(Integer sysKey, String path) throws ClusterStorageException {
        // not supported
        throw new ClusterStorageException("Cannot write to items through the ProxyLoader");
    }

    /* navigation */

    // directory listing
    @Override
	public String[] getClusterContents(Integer sysKey, String path) throws ClusterStorageException {
        try {
            ManageableEntity thisEntity = getIOR(sysKey);
            String contents = thisEntity.queryData(path+"/all");
            StringTokenizer tok = new StringTokenizer(contents, ",");
            String[] result = new String[tok.countTokens()];
            for (int i=0; i<result.length; i++)
                result[i] = tok.nextToken();

            return result;
        } catch (Exception e) {
        	Logger.error(e);
            throw new ClusterStorageException(e.getMessage());
        }
    }

    private ManageableEntity getIOR(Integer sysKey) throws ClusterStorageException {
        if (entities.containsKey(sysKey)) {
            // check the cache
            Logger.msg(7, "ProxyLoader.getIOR() - "+sysKey+" cached.");
            return entities.get(sysKey);
        }

        try {
            Logger.msg(7, "ProxyLoader.getIOR() - Resolving "+sysKey+".");
            org.omg.CORBA.Object ior = lookup.getIOR(new EntityPath(sysKey.intValue()));

            ManageableEntity thisEntity = null;
            try {
                thisEntity = ItemHelper.narrow(ior);
            } catch (org.omg.CORBA.BAD_PARAM ex) {
                try {
                    thisEntity =  AgentHelper.narrow(ior);
                } catch (org.omg.CORBA.BAD_PARAM ex2) {
                    throw new ClusterStorageException ("Could not narrow "+sysKey+" as a known Entity type");
                }
            }

            Logger.msg(7, "ProxyLoader.getIOR() - Found "+sysKey+".");
            entities.put(sysKey, thisEntity);
            return thisEntity;
        } catch (Exception e) {
            throw new ClusterStorageException("Error narrowing "+sysKey+": "+e.getMessage());
        }
    }
}