author | joehw |
Wed, 18 Oct 2017 13:25:49 -0700 | |
changeset 47359 | e1a6c0168741 |
parent 47216 | 71c04702a3d5 |
child 48409 | 5ab69533994b |
permissions | -rw-r--r-- |
6 | 1 |
/* |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
3 |
* @LastModified: Oct 2017 |
6 | 4 |
*/ |
5 |
/* |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
6 | 12 |
* |
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
6 | 14 |
* |
15 |
* Unless required by applicable law or agreed to in writing, software |
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 |
* See the License for the specific language governing permissions and |
|
19 |
* limitations under the License. |
|
20 |
*/ |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
21 |
|
6 | 22 |
package com.sun.org.apache.xml.internal.utils; |
23 |
||
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
24 |
import com.sun.org.apache.xalan.internal.utils.ObjectFactory; |
6 | 25 |
import com.sun.org.apache.xml.internal.res.XMLErrorResources; |
26 |
import com.sun.org.apache.xml.internal.res.XMLMessages; |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
27 |
import java.lang.reflect.InvocationTargetException; |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
28 |
import java.util.ArrayList; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
29 |
import java.util.List; |
6 | 30 |
|
31 |
||
32 |
/** |
|
33 |
* Pool of object of a given type to pick from to help memory usage |
|
34 |
* @xsl.usage internal |
|
35 |
*/ |
|
36 |
public class ObjectPool implements java.io.Serializable |
|
37 |
{ |
|
38 |
static final long serialVersionUID = -8519013691660936643L; |
|
39 |
||
40 |
/** Type of objects in this pool. |
|
41 |
* @serial */ |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
42 |
private final Class<?> objectType; |
6 | 43 |
|
12458 | 44 |
/** Stack of given objects this points to. |
6 | 45 |
* @serial */ |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
46 |
private final List<Object> freeStack; |
6 | 47 |
|
48 |
/** |
|
49 |
* Constructor ObjectPool |
|
50 |
* |
|
51 |
* @param type Type of objects for this pool |
|
52 |
*/ |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
53 |
public ObjectPool(Class<?> type) |
6 | 54 |
{ |
55 |
objectType = type; |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
56 |
freeStack = new ArrayList<>(); |
6 | 57 |
} |
58 |
||
59 |
/** |
|
60 |
* Constructor ObjectPool |
|
61 |
* |
|
62 |
* @param className Fully qualified name of the type of objects for this pool. |
|
63 |
*/ |
|
64 |
public ObjectPool(String className) |
|
65 |
{ |
|
66 |
try |
|
67 |
{ |
|
12458 | 68 |
objectType = ObjectFactory.findProviderClass(className, true); |
6 | 69 |
} |
70 |
catch(ClassNotFoundException cnfe) |
|
71 |
{ |
|
72 |
throw new WrappedRuntimeException(cnfe); |
|
73 |
} |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
74 |
freeStack = new ArrayList<>(); |
6 | 75 |
} |
76 |
||
77 |
||
78 |
/** |
|
79 |
* Constructor ObjectPool |
|
80 |
* |
|
81 |
* |
|
82 |
* @param type Type of objects for this pool |
|
83 |
* @param size Size of vector to allocate |
|
84 |
*/ |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
85 |
public ObjectPool(Class<?> type, int size) |
6 | 86 |
{ |
87 |
objectType = type; |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
88 |
freeStack = new ArrayList<>(size); |
6 | 89 |
} |
90 |
||
91 |
/** |
|
92 |
* Constructor ObjectPool |
|
93 |
* |
|
94 |
*/ |
|
95 |
public ObjectPool() |
|
96 |
{ |
|
97 |
objectType = null; |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
98 |
freeStack = new ArrayList<>(); |
6 | 99 |
} |
100 |
||
101 |
/** |
|
102 |
* Get an instance of the given object in this pool if available |
|
103 |
* |
|
104 |
* |
|
105 |
* @return an instance of the given object if available or null |
|
106 |
*/ |
|
107 |
public synchronized Object getInstanceIfFree() |
|
108 |
{ |
|
109 |
||
110 |
// Check if the pool is empty. |
|
111 |
if (!freeStack.isEmpty()) |
|
112 |
{ |
|
113 |
||
114 |
// Remove object from end of free pool. |
|
12458 | 115 |
Object result = freeStack.remove(freeStack.size() - 1); |
6 | 116 |
return result; |
117 |
} |
|
118 |
||
119 |
return null; |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Get an instance of the given object in this pool |
|
124 |
* |
|
125 |
* |
|
126 |
* @return An instance of the given object |
|
127 |
*/ |
|
128 |
public synchronized Object getInstance() |
|
129 |
{ |
|
130 |
||
131 |
// Check if the pool is empty. |
|
132 |
if (freeStack.isEmpty()) |
|
133 |
{ |
|
134 |
||
135 |
// Create a new object if so. |
|
136 |
try |
|
137 |
{ |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
138 |
return objectType.getConstructor().newInstance(); |
6 | 139 |
} |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
140 |
catch (InstantiationException | IllegalAccessException | SecurityException | |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
141 |
IllegalArgumentException | InvocationTargetException | NoSuchMethodException ex){} |
6 | 142 |
|
143 |
// Throw unchecked exception for error in pool configuration. |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
144 |
throw new RuntimeException(XMLMessages.createXMLMessage( |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44797
diff
changeset
|
145 |
XMLErrorResources.ER_EXCEPTION_CREATING_POOL, null)); |
6 | 146 |
} |
147 |
else |
|
148 |
{ |
|
149 |
||
150 |
// Remove object from end of free pool. |
|
12458 | 151 |
Object result = freeStack.remove(freeStack.size() - 1); |
6 | 152 |
return result; |
153 |
} |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Add an instance of the given object to the pool |
|
158 |
* |
|
159 |
* |
|
160 |
* @param obj Object to add. |
|
161 |
*/ |
|
162 |
public synchronized void freeInstance(Object obj) |
|
163 |
{ |
|
164 |
||
165 |
// Make sure the object is of the correct type. |
|
166 |
// Remove safety. -sb |
|
167 |
// if (objectType.isInstance(obj)) |
|
168 |
// { |
|
12458 | 169 |
freeStack.add(obj); |
6 | 170 |
// } |
171 |
// else |
|
172 |
// { |
|
173 |
// throw new IllegalArgumentException("argument type invalid for pool"); |
|
174 |
// } |
|
175 |
} |
|
176 |
} |