001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server.ejbd;
017:
018: import java.rmi.RemoteException;
019: import java.util.HashMap;
020:
021: import org.apache.openejb.DeploymentInfo;
022: import org.apache.openejb.client.EJBRequest;
023: import org.apache.openejb.util.Messages;
024:
025: public class DeploymentIndex {
026:
027: Messages messages = new Messages("org.apache.openejb.server.ejbd");
028:
029: DeploymentInfo[] deployments = null;
030:
031: HashMap index = null;
032:
033: public DeploymentIndex(DeploymentInfo[] deploymentInfos) {
034: DeploymentInfo[] ds = deploymentInfos;
035:
036: deployments = new DeploymentInfo[ds.length + 1];
037:
038: System.arraycopy(ds, 0, deployments, 1, ds.length);
039:
040: index = new HashMap(deployments.length);
041: for (int i = 1; i < deployments.length; i++) {
042: index.put(deployments[i].getDeploymentID(), new Integer(i));
043: }
044: }
045:
046: public DeploymentInfo getDeployment(EJBRequest req)
047: throws RemoteException {
048:
049: DeploymentInfo info = null;
050:
051: if (req.getDeploymentCode() > 0
052: && req.getDeploymentCode() < deployments.length) {
053: info = deployments[req.getDeploymentCode()];
054: if (info == null) {
055: throw new RemoteException(
056: "The deployement with this ID is null");
057: }
058: req.setDeploymentId((String) info.getDeploymentID());
059: return info;
060: }
061:
062: if (req.getDeploymentId() == null) {
063: throw new RemoteException(
064: "Invalid deployment id and code: id="
065: + req.getDeploymentId() + ": code="
066: + req.getDeploymentCode());
067: }
068:
069: int idCode = getDeploymentIndex(req.getDeploymentId());
070:
071: if (idCode == -1) {
072: throw new RemoteException(
073: "No such deployment id and code: id="
074: + req.getDeploymentId() + ": code="
075: + req.getDeploymentCode());
076: }
077:
078: req.setDeploymentCode(idCode);
079:
080: if (req.getDeploymentCode() < 0
081: || req.getDeploymentCode() >= deployments.length) {
082: throw new RemoteException(
083: "Invalid deployment id and code: id="
084: + req.getDeploymentId() + ": code="
085: + req.getDeploymentCode());
086: }
087:
088: info = deployments[req.getDeploymentCode()];
089: if (info == null) {
090: throw new RemoteException(
091: "The deployement with this ID is null");
092: }
093: return info;
094: }
095:
096: public int getDeploymentIndex(DeploymentInfo deployment) {
097: return getDeploymentIndex((String) deployment.getDeploymentID());
098: }
099:
100: public int getDeploymentIndex(String deploymentID) {
101: Integer idCode = (Integer) index.get(deploymentID);
102:
103: return (idCode == null) ? -1 : idCode.intValue();
104: }
105:
106: public DeploymentInfo getDeployment(String deploymentID) {
107: return getDeployment(getDeploymentIndex(deploymentID));
108: }
109:
110: public DeploymentInfo getDeployment(Integer index) {
111: return (index == null) ? null : getDeployment(index.intValue());
112: }
113:
114: public DeploymentInfo getDeployment(int index) {
115: return deployments[index];
116: }
117: }
|