001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.rGUI.client;
020
021import java.rmi.RemoteException;
022import java.util.concurrent.Semaphore;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import pt.ua.dicoogle.rGUI.interfaces.IUser;
028import pt.ua.dicoogle.rGUI.interfaces.controllers.IDicomSend;
029import pt.ua.dicoogle.rGUI.interfaces.controllers.ISearch;
030
031/**
032 *
033 * @author Samuel Campos <samuelcampos@ua.pt>
034 */
035@Deprecated
036public class UserRefs {
037
038    private ISearch search;
039    private IDicomSend dicomSend;
040
041    private IUser user;
042
043    private static UserRefs instance;
044    private static Semaphore sem = new Semaphore(1, true);
045
046    public static synchronized UserRefs getUserRefs(final IUser user) {
047        try {
048            sem.acquire();
049            if (instance == null) {
050                instance = new UserRefs(user);
051            }
052            sem.release();
053        } catch (InterruptedException ex) {
054            LoggerFactory.getLogger(ClientCore.class).error(ex.getMessage(), ex);
055        }
056        return instance;
057    }
058
059    public static UserRefs getInstance() {
060        return instance;
061    }
062    
063    private UserRefs(IUser user){
064        this.user = user;
065
066        getRefs();
067    }
068
069    /**
070     * This function is responsible for obtaining references to Remote user objects
071     */
072    private void getRefs() {
073        class GetReferences  {
074
075
076            public void run() {
077                try {
078                    //System.out.println("Starting to adquire User Refs");
079
080                    search = user.getSearch();
081                    dicomSend = user.getDicomSend();
082
083                    //System.out.println("User Refs Adquired");
084                } catch (RemoteException ex) {
085                    LoggerFactory.getLogger(AdminRefs.class).error(ex.getMessage(), ex);
086
087                    //TODO: se falhar, ter?? implica????es... o programa cliente deve parar
088                }
089            }
090        }
091        GetReferences getRefs = new GetReferences();
092
093        //starts the Thread that will get the remote administrations object references
094        getRefs.run();
095
096    }
097
098    /**
099     * @return the search
100     */
101    public ISearch getSearch() {
102        return search;
103    }
104
105    /**
106     * @return the dicomSend
107     */
108    public IDicomSend getDicomSend() {
109        return dicomSend;
110    }
111}