summaryrefslogtreecommitdiff
path: root/orktrack/src/net/sf/oreka/orktrack/PortManager.java
blob: 60a40c052a6befc2a6cad575c29562bcf98d7799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

package net.sf.oreka.orktrack;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.sf.oreka.persistent.RecPort;
import net.sf.oreka.persistent.RecPortFace;
import net.sf.oreka.persistent.Service;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

/** Singleton class that manages activity on all capture ports */
public class PortManager {

	HashMap<Integer, Port> portById = new HashMap<Integer, Port>();
	HashMap<String, Port> portByName = new HashMap<String, Port>();
	
	static Logger logger = Logger.getLogger(PortManager.class);
	
	static PortManager portManager = null;
	
	private PortManager () {
	}
	
	public static PortManager instance() {
		if (portManager == null) {
			portManager = new PortManager();
		}
		return portManager;
	}
	
	public Port getPort(String name) {
		return portByName.get(name);
	}
	
	public Port getAndCreatePort(String name, Session hbnSession, Service service) {
		
		Port port = portByName.get(name);
		if (port == null) {
			
			port = createPort(name, hbnSession, service);
		}
		return port; 
	}
	
	public synchronized Port createPort(String name, Session hbnSession, Service service) {
		
		RecPort recPort = null;

		RecPortFace portFace = (RecPortFace)hbnSession.get(RecPortFace.class, name);
		if (portFace == null) {
			portFace  = new RecPortFace();
			portFace.setName(name);
			portFace.setRecPort(recPort);
			portFace.setService(service);
			
			recPort = new RecPort();
			portFace.setRecPort(recPort);
		}
		else {
			portFace.setRecPort(recPort);
			portFace.setService(service);
			recPort = portFace.getRecPort();
		}
		
		hbnSession.save(recPort);
		hbnSession.save(portFace);
		
		logger.info("created port:" + recPort.getId() + " with face:" + name);
		
    	Port port = new Port(recPort);
    	port.portFaces.add(portFace);
    	portById.put(recPort.getId(), port);
    	portByName.put(portFace.getName(), port);
		return port;
	}
	
	// for testing purposes
	public void addPort(String face1, String face2, Session hbnSession) {
		
		RecPort recPort = new RecPort();
		Port port = new Port(recPort);
		RecPortFace portFace1 = new RecPortFace();
		portFace1.setName(face1);
		portFace1.setRecPort(recPort);
		RecPortFace portFace2 = new RecPortFace();
		portFace2.setName(face2);
		portFace2.setRecPort(recPort);
		
		hbnSession.save(recPort);
		hbnSession.save(portFace1);
		hbnSession.save(portFace2);
		
		port.portFaces.add(portFace1);
		port.portFaces.add(portFace2);	
		portByName.put(face1, port);
		portByName.put(face2, port);
		portById.put(1, port);
	}
	
	public boolean initialize() {
		
		Session hbnSession = null;
		boolean success = false;
		try {
			hbnSession = OrkTrack.hibernateManager.getSession();
			Transaction tx = hbnSession.beginTransaction();
			
			Iterator portFaces = hbnSession.createQuery(
        	"from RecPortFace")
        	.list()
        	.iterator();

			while ( portFaces.hasNext() ) {
			    RecPortFace portFace = (RecPortFace)portFaces.next();
			    
			    int portId = portFace.getRecPort().getId();
			    Port port = portById.get(portId);
			    if(port == null) {
			    	RecPort recPort = (RecPort)hbnSession.get(RecPort.class, portId);
			    	if (recPort != null) {
				    	port = new Port(recPort);
				    	portById.put(portId, port);
			    	}
			    }
		    	port.portFaces.add(portFace);
		    	portByName.put(portFace.getName(), port);
			}
			tx.commit();
			success = true;
		}
		catch (Exception e) {
			logger.error("initialize: exception:" + e.getClass().getName());
		}
		finally {
			if(hbnSession != null) {hbnSession.close();}
		}
		return success;
	}
	
	public RecPort getRecPortByFace(String face, Session hbnSession) {
		RecPort port = null;
		List ports = hbnSession.createQuery(
	    "from RecPortFace as pf join pf.recPort as p where pf.name=:face")
	    .setString("face", face)
	    .list();
		if (ports.size() > 0) {
			Object[] row =  (Object[])ports.get(0);
			if (row.length > 1) {
				port = (RecPort)row[1];
			}
		}
		return port;
	}
}