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 */ 017package org.apache.xbean.propertyeditor; 018 019import java.beans.PropertyEditorManager; 020import java.lang.reflect.Type; 021 022/** 023 * The property editor manager. This orchestrates Geronimo usage of 024 * property editors, allowing additional search paths to be added and 025 * specific editors to be registered. 026 * 027 * @version $Rev: 6687 $ 028 */ 029@Deprecated // this is all static and leaks, use PropertyEditorRegistry 030public class PropertyEditors { 031 private static boolean registerWithVM; 032 private static final PropertyEditorRegistry REGISTRY = new PropertyEditorRegistry() { 033 { 034 registerDefaults(); 035 } 036 037 @Override 038 public Converter register(final Converter converter) { 039 final Converter register = super.register(converter); 040 if (registerWithVM) { 041 PropertyEditorManager.registerEditor(converter.getType(), converter.getClass()); 042 final Class<?> sibling = Primitives.findSibling(converter.getType()); 043 if (sibling != null) { 044 PropertyEditorManager.registerEditor(sibling, converter.getClass()); 045 } 046 } 047 return register; 048 } 049 }; 050 051 /** 052 * Are converters registered with the VM PropertyEditorManager. By default 053 * converters are not registered with the VM as this creates problems for 054 * IDE and Spring because they rely in their specific converters being 055 * registered to function properly. 056 */ 057 public static boolean isRegisterWithVM() { 058 return registerWithVM; 059 } 060 061 /** 062 * Sets if converters registered with the VM PropertyEditorManager. 063 * If the new value is true, all currently registered converters are 064 * immediately registered with the VM. 065 */ 066 public static void setRegisterWithVM(boolean registerWithVM) { 067 if (PropertyEditors.registerWithVM != registerWithVM) { 068 PropertyEditors.registerWithVM = registerWithVM; 069 if (registerWithVM) { 070 for (final Converter converter : REGISTRY.getRegistry().values()) { 071 PropertyEditorManager.registerEditor(converter.getType(), converter.getClass()); 072 } 073 } 074 } 075 } 076 077 public static void registerConverter(Converter converter) { 078 REGISTRY.register(converter); 079 } 080 081 public static boolean canConvert(final String type, final ClassLoader classLoader) { 082 if (type == null) { 083 throw new NullPointerException("type is null"); 084 } 085 if (classLoader == null) { 086 throw new NullPointerException("classLoader is null"); 087 } 088 089 try { 090 return REGISTRY.findConverter(Class.forName(type, true, classLoader)) != null; 091 } catch (ClassNotFoundException e) { 092 throw new PropertyEditorException("Type class could not be found: " + type); 093 } 094 } 095 096 public static boolean canConvert(final Class<?> type) { 097 return REGISTRY.findConverter(type) != null; 098 } 099 100 public static String toString(final Object value) throws PropertyEditorException { 101 return REGISTRY.toString(value); 102 } 103 104 public static Object getValue(final String type, final String value, final ClassLoader classLoader) throws PropertyEditorException { 105 return REGISTRY.getValue(type, value, classLoader); 106 107 } 108 109 public static Object getValue(final Type type, final String value) throws PropertyEditorException { 110 return REGISTRY.getValue(type, value); 111 } 112 113 public static PropertyEditorRegistry registry() { 114 return REGISTRY; 115 } 116}