View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jboss.managed;
25  
26  import java.lang.reflect.Type;
27  
28  import org.jboss.metatype.api.types.CompositeMetaType;
29  import org.jboss.metatype.api.types.MetaType;
30  import org.jboss.metatype.api.types.SimpleMetaType;
31  import org.jboss.metatype.api.values.CompositeValue;
32  import org.jboss.metatype.api.values.CompositeValueSupport;
33  import org.jboss.metatype.api.values.MetaValue;
34  import org.jboss.metatype.api.values.MetaValueFactory;
35  import org.jboss.metatype.api.values.SimpleValueSupport;
36  import org.jboss.metatype.plugins.types.MutableCompositeMetaType;
37  import org.jboss.metatype.spi.values.MetaMapper;
38  
39  
40  public class ManagedRepositoryMapper extends MetaMapper<ManagedRepository> {
41  	private static final String NAME = "name"; //$NON-NLS-1$
42  	private static final String VERSION = "version"; //$NON-NLS-1$
43  	private static final MutableCompositeMetaType metaType;
44  	private static final MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
45  	
46  	static {
47  		metaType = new MutableCompositeMetaType(ManagedRepository.class.getName(), "The ModeShape repository instance"); //$NON-NLS-1$
48  		metaType.addItem(NAME, NAME, SimpleMetaType.STRING);
49  		metaType.addItem(VERSION, VERSION, SimpleMetaType.STRING);
50  		metaType.freeze();
51  	}
52  	
53  	@Override
54  	public Type mapToType() {
55  		return ManagedRepository.class;
56  	}
57  	
58  	@Override
59  	public MetaType getMetaType() {
60  		return metaType;
61  	}
62  	
63  	@Override
64  	public MetaValue createMetaValue(MetaType metaType, ManagedRepository object) {
65  		if (object == null)
66  			return null;
67  		if (metaType instanceof CompositeMetaType) {
68  			CompositeMetaType composite = (CompositeMetaType) metaType;
69  			CompositeValueSupport managedRepository = new CompositeValueSupport(composite);
70  			
71  			managedRepository.set(NAME, SimpleValueSupport.wrap(object.getName()));
72  			managedRepository.set(VERSION, SimpleValueSupport.wrap(object.getVersion()));
73  			return managedRepository;
74  		}
75  		throw new IllegalArgumentException("Cannot convert ManagedRepository " + object); //$NON-NLS-1$
76  	}
77  
78  	@Override
79  	public ManagedRepository unwrapMetaValue(MetaValue metaValue) {
80  		if (metaValue == null)
81  			return null;
82  
83  		if (metaValue instanceof CompositeValue) {
84  			CompositeValue compositeValue = (CompositeValue) metaValue;
85  			
86  			ManagedRepository managedRepository = new ManagedRepository();
87  			managedRepository.setName((String) metaValueFactory.unwrap(compositeValue.get(NAME)));
88  			managedRepository.setVersion((String) metaValueFactory.unwrap(compositeValue.get(VERSION)));
89  			return managedRepository;
90  		}
91  		throw new IllegalStateException("Unable to unwrap ManagedRepository " + metaValue); //$NON-NLS-1$
92  	}
93  
94  }