/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.dubbo.registry.support;

import java.io.IOException;
import java.net.ServerSocket;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.ExtensionLoader;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.registry.RegistryService;
import com.alibaba.dubbo.rpc.Exporter;
import com.alibaba.dubbo.rpc.Protocol;
import com.alibaba.dubbo.rpc.RpcConstants;
import com.alibaba.dubbo.rpc.proxy.ProxyFactory;

/**
 * SimpleRegistryExporter
 * 
 * @author william.liangf
 */
public class SimpleRegistryExporter {
    
    private static final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    
    private static final ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    
    public synchronized static Exporter<RegistryService> exportIfAbsent(int port) {
        try {
            new ServerSocket(port).close();
            return export(port);
        } catch (IOException e) {
            return null;
        }
    }
    
    public static Exporter<RegistryService> export(int port) {
        RegistryService registryService = new SimpleRegistryService();
        return protocol.export(proxyFactory.getInvoker(registryService, RegistryService.class, 
                new URL("dubbo", NetUtils.getLocalHost(), port, RegistryService.class.getName())
                .setPath(RegistryService.class.getName())
                .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName())
                .addParameter(Constants.VERSION_KEY, "0.0.0")
                .addParameter(RpcConstants.CLUSTER_STICKY_KEY, "true")
                .addParameter(RpcConstants.CALLBACK_INSTANCES_LIMIT_KEY, "1000")
                .addParameter("ondisconnect", "disconnect")
                .addParameter("subscribe.1.callback", "true")
                .addParameter("unsubscribe.1.callback", "false")));
    }

}
@@ -1,621 +1,467 @@
 /*
+*
 %0D%0A * 
-Copyright 1999-2011 Alibaba Group.
+Project: dubbo-registry
 %0D%0A * 
- 
 %0D%0A * 
-Licensed under the Apache License, Version 2.0 (the %22License%22);%0D%0A * you may not use this file except in compliance with the License.%0D%0A * You may obtain a copy of the License at%0D%0A *  %0D%0A *      http://www.apache.org/licenses/LICENSE-2.0%0D%0A *  %0D%0A * Unless required by applicable law or agreed to in writing, software%0D%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0D%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0D%0A * See
+File Created at 2011-10-13%0D%0A * $Id$%0D%0A * %0D%0A * Copyright 1999-2100 Alibaba.com Corporation Limited.%0D%0A * All rights reserved.%0D%0A *%0D%0A * This software is the confidential and proprietary information of%0D%0A * Alibaba Company. (%22Confidential Information%22).  You shall not%0D%0A * disclose such Confidential Information and shall use it only in%0D%0A * accordance with the terms of
  the 
-L
+l
 icense 
-for the specific language governing permissions and%0D%0A * limitations under the License
+agreement you entered into%0D%0A * with Alibaba.com
 .%0D%0A 
@@ -2109,71 +2109,8 @@
 ))%0D%0A
-                .addParameter(Constants.VERSION_KEY, %220.0.0%22)%0D%0A
     
@@ -2462,8 +2462,10 @@
   %7D%0D%0A%0D%0A%7D
+%0D%0A

Fix monitor bug.