001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2013 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.client.messaging.jmf;
022
023 import java.io.IOException;
024 import java.util.ArrayList;
025 import java.util.HashMap;
026 import java.util.List;
027 import java.util.Map;
028
029 import org.granite.client.messaging.RemoteAlias;
030 import org.granite.client.messaging.jmf.ext.ClientEntityCodec;
031 import org.granite.logging.Logger;
032 import org.granite.messaging.jmf.DefaultCodecRegistry;
033 import org.granite.messaging.jmf.codec.ExtendedObjectCodec;
034 import org.granite.scan.ScannedItem;
035 import org.granite.scan.ScannedItemHandler;
036 import org.granite.scan.Scanner;
037 import org.granite.scan.ScannerFactory;
038 import org.granite.util.JMFAMFUtil;
039
040 /**
041 * @author Franck WOLFF
042 */
043 public class ClientSharedContextFactory {
044
045 private static final Logger log = Logger.getLogger(ClientSharedContextFactory.class);
046
047 private static final String MESSAGING_SCAN_MARKER = "META-INF/messaging-scan.properties";
048
049 private static ClientSharedContext context = null;
050
051 public static synchronized void initialize() {
052 initialize(MESSAGING_SCAN_MARKER);
053 }
054
055 public static synchronized void initialize(String marker) {
056 Map<String, String> clientToServerAliases = new HashMap<String, String>();
057 Scanner scanner = ScannerFactory.createScanner(new MessagingScannedItemHandler(clientToServerAliases), marker);
058 try {
059 scanner.scan();
060 } catch (Exception e) {
061 log.error(e, "Could not scan classpath for configuration");
062 }
063
064 List<ExtendedObjectCodec> extendedCodecs = new ArrayList<ExtendedObjectCodec>();
065 extendedCodecs.add(new ClientEntityCodec());
066 initialize(extendedCodecs, clientToServerAliases);
067 }
068
069 public static synchronized void initialize(List<ExtendedObjectCodec> extendedCodecs, Map<String, String> clientToServerAliases) {
070 context = new DefaultClientSharedContext(new DefaultCodecRegistry(extendedCodecs), null, JMFAMFUtil.AMF_DEFAULT_STORED_STRINGS);
071
072 if (clientToServerAliases != null) {
073 for (Map.Entry<String, String> clientToServerAlias : clientToServerAliases.entrySet())
074 context.registerAlias(clientToServerAlias.getKey(), clientToServerAlias.getValue());
075 }
076 }
077
078 public static synchronized ClientSharedContext getInstance() {
079 if (context == null)
080 context = new DefaultClientSharedContext(new DefaultCodecRegistry(), null, JMFAMFUtil.AMF_DEFAULT_STORED_STRINGS);
081 return context;
082 }
083
084 static class MessagingScannedItemHandler implements ScannedItemHandler {
085
086 final Map<String, String> clientToServerAliases;
087
088 MessagingScannedItemHandler(Map<String, String> clientToServerAliases) {
089 this.clientToServerAliases = clientToServerAliases;
090 }
091
092 @Override
093 public boolean handleMarkerItem(ScannedItem item) {
094 return false;
095 }
096
097 @Override
098 public void handleScannedItem(ScannedItem item) {
099 if ("class".equals(item.getExtension())) {
100 try {
101 Class<?> cls = item.loadAsClass();
102 RemoteAlias alias = cls.getAnnotation(RemoteAlias.class);
103 if (alias != null)
104 clientToServerAliases.put(cls.getName(), alias.value());
105 }
106 catch (ClassFormatError e) {
107 }
108 catch (ClassNotFoundException e) {
109 }
110 catch (IOException e) {
111 log.error(e, "Could not load class: %s", item);
112 }
113 }
114 }
115 }
116 }