001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.webbeans.component;
031:
032: import java.io.Serializable;
033: import java.lang.reflect.*;
034: import java.util.*;
035: import java.util.logging.*;
036: import javax.webbeans.*;
037:
038: import com.caucho.util.*;
039: import com.caucho.webbeans.cfg.*;
040: import com.caucho.webbeans.manager.*;
041:
042: /**
043: * Handle for webbeans serialization
044: */
045: public class WebBeansHandle implements Serializable {
046: private static final L10N L = new L10N(WebBeansHandle.class);
047: private static final Logger log = Logger
048: .getLogger(WebBeansHandle.class.getName());
049:
050: private Class _type;
051: private HashMap<Class, HashMap<String, Object>> _binding;
052:
053: public WebBeansHandle() {
054: }
055:
056: /**
057: * Constructor for known/system singletons
058: */
059: public WebBeansHandle(Class type) {
060: _type = type;
061: _binding = new HashMap<Class, HashMap<String, Object>>();
062: }
063:
064: public WebBeansHandle(Type type, ArrayList<WbBinding> bindingList) {
065: if (type instanceof Class)
066: _type = (Class) type;
067:
068: _binding = new HashMap<Class, HashMap<String, Object>>();
069:
070: for (WbBinding binding : bindingList) {
071: Class annType = binding.getBindingClass();
072: HashMap<String, Object> valueMap = new HashMap<String, Object>();
073:
074: for (WbBinding.WbBindingValue value : binding
075: .getValueList()) {
076: valueMap.put(value.getName(), value.getValue());
077: }
078:
079: _binding.put(annType, valueMap);
080: }
081: }
082:
083: /**
084: * Deserialization resolution
085: */
086: public Object readResolve() {
087: try {
088: ArrayList<Binding> bindingList = new ArrayList<Binding>();
089:
090: for (Map.Entry<Class, HashMap<String, Object>> entry : _binding
091: .entrySet()) {
092: Class type = entry.getKey();
093: HashMap<String, Object> valueMap = entry.getValue();
094:
095: Binding binding = new Binding(type);
096: for (Map.Entry<String, Object> bindingEntry : valueMap
097: .entrySet()) {
098: binding.put(bindingEntry.getKey(), bindingEntry
099: .getValue());
100: }
101:
102: bindingList.add(binding);
103: }
104:
105: WebBeansContainer webBeans = WebBeansContainer.create();
106: ComponentFactory comp = webBeans.bindByBindings("", _type,
107: bindingList);
108:
109: if (comp != null)
110: return comp.get();
111: else {
112: log
113: .warning(L
114: .l(
115: "'{0}' is an unknown WebBean at deserialization",
116: this ));
117: return null;
118: }
119: } catch (RuntimeException e) {
120: throw e;
121: } catch (Exception e) {
122: throw new RuntimeException(e);
123: }
124: }
125:
126: public String toString() {
127: return getClass().getName() + "[" + _type + "," + _binding
128: + "]";
129: }
130: }
|