summaryrefslogtreecommitdiff
path: root/orktrack/src/net/sf/oreka/orktrack/TapeManager.java
blob: 66e15ae5253fb60e27d507d03a6d5bcb513f7b49 (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
package net.sf.oreka.orktrack;

import java.util.Date;

import net.sf.oreka.orktrack.messages.TapeMessage;
import net.sf.oreka.persistent.RecSegment;
import net.sf.oreka.persistent.RecTape;
import net.sf.oreka.persistent.Service;
import net.sf.oreka.persistent.User;

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

public class TapeManager {
	
	static Logger logger = Logger.getLogger(TapeManager.class);
	
	static TapeManager tapeManager = null;
	
	private TapeManager () {
	}
	
	public static TapeManager instance() {
		if (tapeManager == null) {
			tapeManager = new TapeManager();
		}
		return tapeManager;
	}
	
	/**
	 * @param tapeMessage
	 * @param hbnSession
	 * @param srv
	 * @return	false if the tape is rejected and should be deleted, otherwise true
	 */
	public boolean notifyTapeMessage(TapeMessage tapeMessage, Session hbnSession, Service srv) {
		
		boolean keepTape = true;
		
		if (tapeMessage.getStage() == TapeMessage.CaptureStage.START) {
			; // tape start message
		}
		else {
			// Tape stop message
			long date = ((long)tapeMessage.getTimestamp()) * 1000;
			Date timestamp = new Date(date);
            
			// create a new tape record
			RecTape recTape = new RecTape();
			recTape.setDirection(tapeMessage.getDirection());
			recTape.setDuration(tapeMessage.getDuration());
			recTape.setExpiryTimestamp(new Date());
			recTape.setFilename(tapeMessage.getFilename());
			recTape.setLocalParty(tapeMessage.getLocalParty());
			recTape.setRecPortName(tapeMessage.getCapturePort());
			recTape.setRemoteParty(tapeMessage.getRemoteParty());
			recTape.setTimestamp(timestamp);
			recTape.setService(srv);
			hbnSession.save(recTape);
			logger.info("Written tape:" + tapeMessage.getRecId() + " as " + recTape.getId());
			
			RecSegment recSegment = new RecSegment();
			recSegment.setTimestamp(timestamp);
			recSegment.setDirection(tapeMessage.getDirection());
			recSegment.setDuration(tapeMessage.getDuration());
			recSegment.setRemoteParty(tapeMessage.getRemoteParty());
			recSegment.setLocalParty(tapeMessage.getLocalParty());
			recSegment.setLocalEntryPoint(tapeMessage.getLocalEntryPoint());
			recSegment.setRecTape(recTape);
			recSegment.setRecPortName(recTape.getRecPortName());
			
			if(tapeMessage.getLocalParty() != "") {
				User user = UserManager.instance().getByLoginString(tapeMessage.getLocalParty(), hbnSession);
				recSegment.setUser(user);
			}
			if (ProgramManager.instance().filterSegmentAgainstAllPrograms(recSegment, hbnSession)) {
				hbnSession.save(recSegment);
				logger.info("Written segment:" + tapeMessage.getRecId() + " as " + recSegment.getId());
			}
			else {
				logger.info("Tape:" + tapeMessage.getRecId() + " not retained by any program");
				keepTape = false;
			}
		}
		return keepTape;
	}
}