]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/global/GlobalShare.java
Initial import.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / global / GlobalShare.java
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13 ShareObject.java
14
15 Abstract:
16
17 --*/
18 package org.tianocore.build.global;
19
20 import java.util.*;
21
22 import org.apache.tools.ant.*;
23 import org.apache.tools.ant.types.DataType;
24
25 public class GlobalShare extends DataType implements DynamicConfigurator {
26 private static final HashMap<String, Object> objStorage = new HashMap<String, Object>();
27
28 private DataObjectOp op;
29
30 private String objName;
31
32 private Object objInst;
33
34 private String objClassPackage = "org.tianocore";
35
36 public GlobalShare () {
37
38 }
39
40 public GlobalShare (String objName) {
41 this.objName = objName;
42 this.objInst = objStorage.get(this.objName);
43 }
44
45 public GlobalShare (String objName, Object obj) {
46 this.objName = objName;
47 this.objInst = obj;
48 objStorage.put(this.objName, this.objInst);
49 }
50
51 public Object createDynamicElement(String name) throws BuildException {
52 String className = objClassPackage + "." + name;
53 log("GlobalShare.createDynamicElement(" + name + ")",
54 Project.MSG_VERBOSE);
55 try {
56 objInst = Class.forName(className).newInstance();
57 } catch (ClassNotFoundException e) {
58 throw new BuildException("class name is not found");
59 } catch (InstantiationException e) {
60 throw new BuildException("the class cannnot be instantiated");
61 } catch (IllegalAccessException e) {
62 throw new BuildException("cannot access the class");
63 }
64
65 return objInst;
66 }
67
68 public void setDynamicAttribute(String name, String value)
69 throws BuildException {
70 log("name = " + name + " value = " + value, Project.MSG_VERBOSE);
71 throw new BuildException();
72 }
73
74 public void setName(String name) {
75 this.objName = name;
76 if (this.op != null) {
77 issueOperation();
78 }
79 }
80
81 public String getName() {
82 return this.objName;
83 }
84
85 public void setPackage(String name) {
86 log("ShareObject.setPackage(" + name + ")", Project.MSG_VERBOSE);
87 this.objClassPackage = name;
88 }
89
90 public String getPackage() {
91 return this.objClassPackage;
92 }
93
94 public void setOperation(String opName) {
95 log("ShareObject.setOperation(" + opName + ")", Project.MSG_VERBOSE);
96 this.op = DataObjectOp.formString(opName);
97
98 if (this.objName != null) {
99 issueOperation();
100 }
101 }
102
103 public String getOperation() {
104 return this.op.toString();
105 }
106
107 public void issueOperation() {
108 if (this.op == DataObjectOp.ADD) {
109
110 log("ShareObject: adding ... " + this.objName, Project.MSG_VERBOSE);
111 objStorage.put(this.objName, this.objInst);
112
113 } else if (this.op == DataObjectOp.GET) {
114
115 log("ShareObject: fetching ... " + this.objName,
116 Project.MSG_VERBOSE);
117 objInst = objStorage.get(objName);
118
119 } else if (this.op == DataObjectOp.DEL) {
120
121 log("ShareObject: removing ... " + this.objName,
122 Project.MSG_VERBOSE);
123 objInst = objStorage.remove(objName);
124
125 } else {
126 throw new BuildException("not supported operation");
127 }
128 }
129
130 public Object get() {
131 return this.objInst;
132 }
133
134 public static int getObjectNum() {
135 return objStorage.size();
136 }
137
138 public static Object add(String objName, Object obj) {
139 return objStorage.put(objName, obj);
140 }
141
142 public static Object retrieve(String objName) {
143 return objStorage.get(objName);
144 }
145
146 public static Object remove(String objName) {
147 return objStorage.remove(objName);
148 }
149
150 public static void empty() {
151 objStorage.clear();
152 }
153 }
154
155 class DataObjectOp {
156 private static final HashMap<String, DataObjectOp> opMap = new HashMap<String, DataObjectOp>();
157
158 private final String opName;
159
160 private DataObjectOp (String name) {
161 this.opName = name;
162 opMap.put(this.opName, this);
163 }
164
165 public String toString() {
166 return opName;
167 }
168
169 public static DataObjectOp formString(String opName) {
170 return opMap.get(opName);
171 }
172
173 public static final DataObjectOp ADD = new DataObjectOp("ADD");
174
175 public static final DataObjectOp GET = new DataObjectOp("GET");
176
177 public static final DataObjectOp DEL = new DataObjectOp("DEL");
178 }