KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > channels > ApplicationFileChannel


1 package com.sslexplorer.agent.channels;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import com.maverick.multiplex.Channel;
11 import com.maverick.multiplex.ChannelOpenException;
12 import com.maverick.util.ByteArrayReader;
13 import com.sslexplorer.agent.AgentTunnel;
14 import com.sslexplorer.boot.Util;
15 import com.sslexplorer.extensions.ExtensionDescriptor;
16 import com.sslexplorer.extensions.store.ExtensionStore;
17 import com.sslexplorer.policyframework.LaunchSession;
18 import com.sslexplorer.policyframework.LaunchSessionFactory;
19
20 /**
21  * Channel implementation used for download files required by the <i>Agent</i>
22  * to launch an <i>Application Shortcut</i>.
23  *
24  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
25  */

26 public class ApplicationFileChannel extends Channel implements Runnable JavaDoc {
27     final static Log log = LogFactory.getLog(ApplicationFileChannel.class);
28
29     /**
30      * Channel type identifier
31      */

32     public static final String JavaDoc CHANNEL_TYPE = "applicationFile@3sp.com";
33
34     String JavaDoc name;
35     String JavaDoc launchId;
36     String JavaDoc filename;
37     File JavaDoc file;
38     FileInputStream JavaDoc in;
39     Thread JavaDoc thread;
40     AgentTunnel agent;
41
42     /**
43      * Constructor.
44      *
45      * @param agent agent
46      *
47      */

48     public ApplicationFileChannel(AgentTunnel agent) {
49         super(CHANNEL_TYPE, 32768, 0);
50         this.agent = agent;
51     }
52
53     /* (non-Javadoc)
54      * @see com.maverick.multiplex.Channel#open(byte[])
55      */

56     @Override JavaDoc
57     public byte[] open(byte[] data) throws IOException JavaDoc, ChannelOpenException {
58
59         try {
60             ByteArrayReader reader = new ByteArrayReader(data);
61             name = reader.readString();
62             launchId = reader.readString();
63             filename = reader.readString();
64
65             LaunchSession launchSession = LaunchSessionFactory.getInstance().getLaunchSession(launchId);
66             if (launchSession == null) {
67                 throw new ChannelOpenException(ChannelOpenException.CHANNEL_REFUSED, "No launch session with ID " + launchId + ", cannot read file " + filename + " for " + name);
68             }
69
70             launchSession.checkAccessRights(null, agent.getSession());
71
72             ExtensionDescriptor descriptor = ExtensionStore.getInstance().getExtensionDescriptor(name);
73             if (!descriptor.containsFile(filename))
74                 throw new ChannelOpenException(ChannelOpenException.CHANNEL_REFUSED, "Application does not contain file " + filename + " in extension " + name);
75
76             this.file = descriptor.getFile(filename);
77             this.in = new FileInputStream JavaDoc(file);
78
79         } catch (Exception JavaDoc e) {
80             throw new ChannelOpenException(ChannelOpenException.CHANNEL_REFUSED, e.getMessage());
81         }
82
83         return null;
84
85     }
86
87     /* (non-Javadoc)
88      * @see com.maverick.multiplex.Channel#onChannelData(byte[], int, int)
89      */

90     public void onChannelData(byte[] buf, int off, int len) {
91     }
92
93     /* (non-Javadoc)
94      * @see com.maverick.multiplex.Channel#create()
95      */

96     @Override JavaDoc
97     public byte[] create() throws IOException JavaDoc {
98         return null;
99     }
100
101     /* (non-Javadoc)
102      * @see com.maverick.multiplex.Channel#onChannelOpen(byte[])
103      */

104     @Override JavaDoc
105     public void onChannelOpen(byte[] data) {
106         thread = new Thread JavaDoc(this);
107         thread.start();
108     }
109
110     /* (non-Javadoc)
111      * @see com.maverick.multiplex.Channel#onChannelClose()
112      */

113     @Override JavaDoc
114     public void onChannelClose() {
115         // TODO Auto-generated method stub
116

117     }
118
119     /* (non-Javadoc)
120      * @see java.lang.Runnable#run()
121      */

122     public void run() {
123         try {
124             Util.copy(in, getOutputStream());
125         } catch (Exception JavaDoc ex) {
126             log.error("Failed to copy file contents.", ex);
127         } finally {
128             close();
129         }
130     }
131
132 }
133
Popular Tags