001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * $Id$ 019 */ 020 021package org.nuxeo.runtime.model.impl; 022 023import java.io.IOException; 024import java.io.InputStream; 025 026import org.nuxeo.common.xmap.Context; 027import org.nuxeo.common.xmap.XMap; 028import org.nuxeo.common.xmap.XValueFactory; 029import org.nuxeo.runtime.Version; 030import org.nuxeo.runtime.model.ComponentName; 031import org.nuxeo.runtime.model.RuntimeContext; 032 033/** 034 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 035 */ 036public class ComponentDescriptorReader { 037 038 private final XMap xmap; 039 040 public ComponentDescriptorReader() { 041 xmap = new XMap(); 042 xmap.setValueFactory(ComponentName.class, new XValueFactory() { 043 @Override 044 public Object deserialize(Context context, String value) { 045 return new ComponentName(value); 046 } 047 048 @Override 049 public String serialize(Context context, Object value) { 050 if (value != null) { 051 return value.toString(); 052 } 053 return null; 054 } 055 }); 056 xmap.setValueFactory(Version.class, new XValueFactory() { 057 @Override 058 public Object deserialize(Context context, String value) { 059 return Version.parseString(value); 060 } 061 062 @Override 063 public String serialize(Context context, Object value) { 064 if (value != null) { 065 return value.toString(); 066 } 067 return null; 068 } 069 }); 070 xmap.register(RegistrationInfoImpl.class); 071 } 072 073 public RegistrationInfoImpl read(RuntimeContext ctx, InputStream in) throws IOException { 074 Object[] result = xmap.loadAll(new XMapContext(ctx), in); 075 if (result.length > 0) { 076 return (RegistrationInfoImpl) result[0]; 077 } 078 return null; 079 } 080 081}