3 Copyright (C) 2007 Jehan Monnier ()
5 P2pProxyAccountManagement.java - .
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 package org.linphone.p2pproxy.core;
24 import java.io.IOException;
25 import java.util.List;
27 import net.jxta.discovery.DiscoveryService;
28 import net.jxta.document.Advertisement;
29 import net.jxta.document.AdvertisementFactory;
30 import net.jxta.id.ID;
31 import net.jxta.id.IDFactory;
33 import org.apache.log4j.Logger;
34 import org.linphone.p2pproxy.api.P2pProxyException;
35 import org.linphone.p2pproxy.api.P2pProxyUserNotFoundException;
37 import org.linphone.p2pproxy.api.P2pProxyUserAlreadyExistException;
39 public class P2pProxyAccountManagement implements ServiceProvider, P2pProxyAccountManagementMBean {
40 private final static Logger mLog = Logger.getLogger(P2pProxyAccountManagement.class);
41 protected final JxtaNetworkManager mJxtaNetworkManager;
43 public P2pProxyAccountManagement() {
44 mJxtaNetworkManager = null;
47 * @param jxtaNetworkManager
49 public P2pProxyAccountManagement(final JxtaNetworkManager jxtaNetworkManager) {
51 mJxtaNetworkManager = jxtaNetworkManager;
54 public void start(long aTimeOut) throws P2pProxyException {
55 mLog.info("P2pProxyAccountManagementMBean started");
59 mLog.info("P2pProxyAccountManagementMBean stopped");
62 public void createAccount(String aUserName) throws P2pProxyException, P2pProxyUserAlreadyExistException {
63 // 1 check if already exist
64 if (isValidAccount(aUserName) == false) {
66 // 2 creates and remote publish
67 P2pUserProfileAdvertisement lP2pUserProfileAdvertisement = (P2pUserProfileAdvertisement) AdvertisementFactory.newAdvertisement(P2pUserProfileAdvertisement.getAdvertisementType());
69 lP2pUserProfileAdvertisement.setID(IDFactory.newCodatID(mJxtaNetworkManager.getPeerGroup().getPeerGroupID()));
70 lP2pUserProfileAdvertisement.setUserName(aUserName);
72 mJxtaNetworkManager.getPeerGroup().getDiscoveryService().publish(lP2pUserProfileAdvertisement,DiscoveryService.INFINITE_LIFETIME,DiscoveryService.DEFAULT_EXPIRATION);
73 } catch (IOException e1) {
74 throw new P2pProxyException(e1);
76 mJxtaNetworkManager.getPeerGroup().getDiscoveryService().remotePublish(lP2pUserProfileAdvertisement, DiscoveryService.NO_EXPIRATION);
77 mLog.debug("publishing P2pUserProfileAdvertisement :"+lP2pUserProfileAdvertisement);
79 throw new P2pProxyUserAlreadyExistException(aUserName);
84 public void deleteAccount(String aUserName) throws P2pProxyException, P2pProxyUserNotFoundException {
85 // 1 check if already exist
87 List<? extends Advertisement> lAdvertisements = mJxtaNetworkManager.getAdvertisementList(null, P2pUserProfileAdvertisement.USER_NAME_TAG, aUserName, true);
88 if (lAdvertisements.isEmpty()) throw new P2pProxyUserNotFoundException (aUserName +" not found");
90 // 2 local and remote publish to 0
91 mJxtaNetworkManager.getPeerGroup().getDiscoveryService().flushAdvertisement(lAdvertisements.get(0));
92 mJxtaNetworkManager.getPeerGroup().getDiscoveryService().remotePublish(lAdvertisements.get(0), 0);
94 } catch (P2pProxyAdvertisementNotFoundException e) {
96 } catch (InterruptedException e) {
97 throw new P2pProxyException(e);
98 } catch (IOException e) {
99 throw new P2pProxyException(e);
104 public boolean isValidAccount(String aUserName) throws P2pProxyException {
105 boolean lStatus = false;
107 if (mJxtaNetworkManager.getAdvertisementList(null, P2pUserProfileAdvertisement.USER_NAME_TAG, aUserName, true).size() >0 ) {
110 } catch (P2pProxyAdvertisementNotFoundException e) {
112 }catch (Exception e) {
113 mLog.error("cannot check acount",e);