summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lookup/EntityPath.java
blob: deb44a3fb3fb3b1c9d678f96a1ac3c4e288fea38 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**************************************************************************
 * EntityPath.java
 *
 * $Revision: 1.14 $
 * $Date: 2006/03/03 13:52:21 $
 *
 * Copyright (C) 2001 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

package com.c2kernel.lookup;

import java.util.ArrayList;

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.process.Gateway;


/**
* Extends Path to enforce SystemKey structure and support int form
*
* @version $Revision: 1.14 $ $Date: 2006/03/03 13:52:21 $
* @author  $Author: abranson $
**/
public class EntityPath extends Path
{
    // no of components in a syskey
    public static final int elementNo = 3;
    // no of digits in a syskey component
    public static final int elementLen = 3;
    // maximum possible int syskey
    public static final int maxSysKey = (int)Math.pow(10, elementNo*elementLen)-1;
    protected static String mTypeRoot;
    // ior is stored in here when it is resolved
    protected org.omg.CORBA.Object   mIOR         = null;
    /*
     * From syskey int
     * Note no EntityPath constructors allow setting of CONTEXT or ENTITY:
     * The object decides that for itself from the number of components
     */

    public EntityPath(int syskey) throws InvalidEntityPathException {
        super();
        setSysKey(syskey);
    }

    /*
    */
    public EntityPath()
    {
        super();
    }


    /*
    */
    public EntityPath(String[] path) throws InvalidEntityPathException
    {
        super(path, Path.CONTEXT); // dummy - it will get replaced in checkSysPath()
        checkSysPath();
    }

    /*
    */
    public EntityPath(String path) throws InvalidEntityPathException
    {
        super(path, Path.CONTEXT);
        checkSysPath();
    }

    /*
    */
    public EntityPath(EntityPath parent, String child) throws InvalidEntityPathException {
        super(parent, child);
        checkSysPath();
    }

    // EntityPaths root in /entity
    @Override
	public String getRoot() {
        return "entity";
    }

    public static void setTypeRoot(String root) {
    	mTypeRoot = root;
    }
    
    public static String getTypeRoot() {
    	return mTypeRoot;
    }
    
    @Override
	public EntityPath getEntity() throws ObjectNotFoundException {
        return this;
    }

    public byte[] getOID() {
        if (mSysKey == Path.INVALID) return null;
        return String.valueOf(mSysKey).getBytes();
    }

    /*************************************************************************/

    /** Returns int form of syskey (if possible)
    */
    @Override
	public int getSysKey() {
        if (mSysKey == Path.INVALID && mType == Path.ENTITY)
            try {
                if (mPath.length != elementNo)
                    throw new InvalidEntityPathException("Incorrect number of components for a system key");
                mSysKey = 0;
                for (String keypart : mPath) {
                    if (keypart.length()!=elementLen+1)
                        throw new InvalidEntityPathException("Component '"+keypart+"' is not the correct size for a system key");
                    for(int j=1; j<elementLen+1; j++) // skip the 'd' prefix
                    {
                        char c = keypart.charAt(j);
                        if((c >= '0') && (c <='9'))
                            mSysKey = mSysKey*10 + c - '0';
                        else
                            throw new InvalidEntityPathException("Component '"+keypart+"' contained a non-numeric char (excluding first char 'd').");

                    }
                }
            } catch (InvalidEntityPathException ex) {
                mSysKey = Path.INVALID;
            }
        return mSysKey;
    }

    /** Sets path from int syskey
    */
    public void setSysKey(int sysKey) throws InvalidEntityPathException
    {
        if (sysKey < 0 || sysKey > maxSysKey)
            throw new InvalidEntityPathException("System key "+sysKey+" out of range");
        String stringPath = Integer.toString(sysKey);
        ArrayList<String> newKey = new ArrayList<String>();
        for (int i=0; i<elementNo; i++) {
            StringBuffer nextComponent = new StringBuffer("d");
            int start = stringPath.length()-elementLen;
            if (start < 0) {
                nextComponent.append("000000".substring(0,Math.abs(start))).append(stringPath);
                stringPath = "";
            }
            else {
                nextComponent.append(stringPath.substring(start));
                stringPath = stringPath.substring(0, start);
            }
            newKey.add(0, nextComponent.toString());
        }

        mPath = (newKey.toArray(mPath));
        mSysKey = sysKey;
        mStringPath = null;
        mDN = null;
        mType = Path.ENTITY;
        checkSysPath();
    }

    public void checkSysPath() throws InvalidEntityPathException {
        if (mPath.length > elementNo)
            throw new InvalidEntityPathException("EntityPath cannot have more than "+elementNo+" components: "+toString());
        if (mPath.length == elementNo)
            mType = Path.ENTITY;
        else
            mType = Path.CONTEXT;
    }
    
    // lookup sets the IOR
    public void setIOR(org.omg.CORBA.Object IOR) {
        mIOR = IOR;
        if (IOR == null) mType = Path.CONTEXT;
        else mType = Path.ENTITY;
    }
    
    /** Queries the lookup for the IOR
     */
    public org.omg.CORBA.Object getIOR() {
        org.omg.CORBA.Object newIOR = null;
        if (mIOR==null) { // if not cached try to resolve
            Lookup myLookup = Gateway.getLookup();
            try {
                newIOR = myLookup.resolve(this);
            } catch (ObjectNotFoundException ex) {
            }
            setIOR(newIOR);
        }
        return mIOR;
    }
}