summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/storage/USBWorkspaceManager.java
blob: dba5b6d2cd3aa73eaa3c32e1dae7d01f1d58a8de (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
172
173
174
175
176
177
178
179
package xlogo.storage;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.samuelcampos.usbdrivedectector.USBDeviceDetectorManager;
import net.samuelcampos.usbdrivedectector.USBStorageDevice;
import net.samuelcampos.usbdrivedectector.events.IUSBDriveListener;
import net.samuelcampos.usbdrivedectector.events.USBStorageEvent;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import xlogo.storage.workspace.WorkspaceConfig;
import xlogo.storage.workspace.WorkspaceConfigJSONSerializer;
import xlogo.storage.workspace.WorkspaceConfig.WorkspaceProperty;

public class USBWorkspaceManager {
	private static Logger				logger	= LogManager.getLogger(USBWorkspaceManager.class.getSimpleName());
	private USBDeviceDetectorManager	driveDetector;
	private WorkspaceContainer wm;
	private Map<String, StorableObject<WorkspaceConfig, WorkspaceProperty>> usbWorkspaces;
	
	public USBWorkspaceManager(WorkspaceContainer workspaceManager) {
		this.wm = workspaceManager;
		usbWorkspaces = new HashMap<String, StorableObject<WorkspaceConfig, WorkspaceProperty>>();
	}
	
	/**
	 * Detect External Drives
	 */
	public void init() {
		if (driveDetector != null) { return; }
		
		driveDetector = new USBDeviceDetectorManager(800);
		
		for (USBStorageDevice rmDevice : driveDetector.getRemovableDevices()) {
			if (rmDevice.canRead() && rmDevice.canWrite()) {
				addUSBDrive(rmDevice);
			}
		}
		
		driveDetector.addDriveListener(new IUSBDriveListener(){
			
			@Override
			public void usbDriveEvent(USBStorageEvent event) {
				USBStorageDevice rmDevice = event.getStorageDevice();
				switch (event.getEventType()) {
					case CONNECTED:
						addUSBDrive(rmDevice);
						break;
					case REMOVED:
						removeUSBDrive(rmDevice);
						break;
				}
			}
		});
	}
	
	public void stop() {
		driveDetector.stop();
	}
	
	private void addUSBDrive(USBStorageDevice rmDevice) {
		logger.trace("USB Drive attached: " + rmDevice);
		StorableObject<WorkspaceConfig, WorkspaceProperty> usbwc = initUSBDrive(rmDevice);
		if (usbwc != null && usbwc.get() == null) {
			// TODO fail case
			return;
		}
		usbWorkspaces.put(usbwc.get().getWorkspaceName(), usbwc);
		wm.add(usbwc);
	}
		
	private void removeUSBDrive(USBStorageDevice rmDevice) {
		logger.trace("USB Drive removed: " + rmDevice);
		String deviceName = rmDevice.getSystemDisplayName();
		StorableObject<WorkspaceConfig, WorkspaceProperty> usbwc = usbWorkspaces.get(deviceName);
		wm.remove(usbwc);
		// remove cached version from the same workspace
		//File usbRoot = rmDevice.getRootDirectory();
		//File usbWsDir = StorableObject.getDirectory(usbRoot, WorkspaceConfig.USB_DEFAULT_WORKSPACE);
		//ObjectCache.getInstance().remove(usbWsDir, WorkspaceConfig.class);
	}
	
	private StorableObject<WorkspaceConfig, WorkspaceProperty> initUSBDrive(USBStorageDevice rmDevice) {
		logger.trace("Initializing USB Drive: " + rmDevice.getDeviceName());
		
		String deviceName = rmDevice.getSystemDisplayName();
		
		File usbRoot = rmDevice.getRootDirectory();
		File usbWsDir = StorableObject.getDirectory(usbRoot, WorkspaceConfig.USB_DEFAULT_WORKSPACE);
		
		StorableObject<WorkspaceConfig, WorkspaceProperty> usbwc = WorkspaceConfigJSONSerializer.createOrLoad(usbWsDir, true, true);
		try {
			usbwc = usbwc.createOrLoad();
			usbwc.get().setDirectory(usbWsDir);
			usbwc.get().setWorkspaceName(deviceName);
		}
		catch (Exception e) {
			// TODO
			e.printStackTrace();
			return null;
		}
		
		return usbwc;
	}
	
	public StorableObject<WorkspaceConfig, WorkspaceProperty> createOrLoad(String deviceName) {
		logger.trace("Initializing USB Drive: " + deviceName);
		File usbRoot = null;
		for (USBStorageDevice device : driveDetector.getRemovableDevices()) {
			if (deviceName.equals(device.getSystemDisplayName())) {
				usbRoot = device.getRootDirectory();
				break;
			}
		}
		if (usbRoot == null) { return null; }
		
		File usbWsDir = StorableObject.getDirectory(usbRoot, WorkspaceConfig.USB_DEFAULT_WORKSPACE);
		
		StorableObject<WorkspaceConfig, WorkspaceProperty> wsc = WorkspaceConfigJSONSerializer.createOrLoad(usbWsDir, true);
		wsc.get().setWorkspaceName(deviceName);
		return wsc;
	}
	
	public String getFirstUSBWorkspace(String[] workspaceNamesc) {
		for (String ws : workspaceNamesc) {
			if (isUSBDrive(ws)) {
				return ws; 
			}
		}
		return null;
	}
	
	public boolean isUSBDrive(String workspaceName) {
		if (driveDetector == null) { return false; }
		List<USBStorageDevice> devices = driveDetector.getRemovableDevices();
		logger.trace("Is '" + workspaceName + "' on a USB Drive?");
		for (USBStorageDevice device : devices) {
			if (workspaceName.contains(device.getSystemDisplayName())) {
				logger.trace("\t = Yes, corresponding USB Device found.");
				return true;
			}
			else {
				logger.trace("\t Does not correspond to " + device.getSystemDisplayName());
			}
		}
		
		logger.trace("\t = No, could not find corresponding USB Drive.");
		return false;
	}
	
	public File getWorkspaceDirectory(String workspaceName){
		if (driveDetector == null) { return null; }
		if(usbWorkspaces.containsKey(workspaceName)){
			return usbWorkspaces.get(workspaceName).getLocation();
		}
		List<USBStorageDevice> devices = driveDetector.getRemovableDevices();
		logger.trace("Is '" + workspaceName + "' on a USB Drive?");
		for (USBStorageDevice device : devices) {
			if (workspaceName.contains(device.getSystemDisplayName())) {
				logger.trace("\t = Yes, corresponding USB Device found.");
				return new File(device.getRootDirectory().toString() + File.separator + WorkspaceConfig.USB_DEFAULT_WORKSPACE);
			}
			else {
				logger.trace("\t Does not correspond to " + device.getSystemDisplayName());
			}
		}
		return null;
	}
	
	public interface WorkspaceContainer {
		public void add(StorableObject<WorkspaceConfig, WorkspaceProperty> wc);
		public void remove(StorableObject<WorkspaceConfig, WorkspaceProperty> wc);
	}
}