01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.x.imageio.metadata;
19:
20: import java.lang.reflect.Method;
21: import java.security.AccessController;
22: import java.security.PrivilegedAction;
23:
24: import javax.imageio.metadata.IIOMetadataFormat;
25: import javax.imageio.metadata.IIOMetadataFormatImpl;
26:
27: public class IIOMetadataUtils {
28: private IIOMetadataUtils() {
29: }
30:
31: public static IIOMetadataFormat instantiateMetadataFormat(
32: String formatName, boolean standardFormatSupported,
33: String nativeMetadataFormatName,
34: String nativeMetadataFormatClassName,
35: String[] extraMetadataFormatNames,
36: String[] extraMetadataFormatClassNames) {
37: if (formatName == null) {
38: throw new IllegalArgumentException("formatName == null!");
39: }
40: if (formatName
41: .equals(IIOMetadataFormatImpl.standardMetadataFormatName)) {
42: if (standardFormatSupported) {
43: return IIOMetadataFormatImpl
44: .getStandardFormatInstance();
45: }
46: }
47:
48: String className = null;
49:
50: if (formatName.equals(nativeMetadataFormatName)) {
51: className = nativeMetadataFormatClassName;
52: } else if (extraMetadataFormatNames != null) {
53: for (int i = 0; i < extraMetadataFormatNames.length; i++) {
54: if (formatName.equals(extraMetadataFormatNames[i])) {
55: className = extraMetadataFormatClassNames[i];
56: break;
57: }
58: }
59: }
60:
61: if (className == null) {
62: throw new IllegalArgumentException(
63: "Unsupported format name");
64: }
65:
66: // Get the context class loader and try to use it first
67: ClassLoader contextClassloader = AccessController
68: .doPrivileged(new PrivilegedAction<ClassLoader>() {
69: public ClassLoader run() {
70: return Thread.currentThread()
71: .getContextClassLoader();
72: }
73: });
74:
75: Class cls;
76:
77: try {
78: cls = Class.forName(className, true, contextClassloader);
79: } catch (ClassNotFoundException e) {
80: try {
81: // Use current class loader
82: cls = Class.forName(className);
83: } catch (ClassNotFoundException e1) {
84: throw new IllegalStateException("Can't obtain format");
85: }
86: }
87:
88: try {
89: Method getInstance = cls.getMethod("getInstance");
90: return (IIOMetadataFormat) getInstance.invoke(null);
91: } catch (Exception e) {
92: IllegalStateException e1 = new IllegalStateException(
93: "Can't obtain format");
94: e1.initCause(e); // Add some details to the message
95: throw e1;
96: }
97: }
98: }
|