1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50:
56: public class DataHandler
57: implements Transferable
58: {
59:
60: private static final DataFlavor[] NO_FLAVORS = new DataFlavor[0];
61: private static DataContentHandlerFactory factory = null;
62:
63: private final DataSource dataSource;
64: private DataSource objDataSource;
65: private Object object;
66: private String objectMimeType;
67: private CommandMap currentCommandMap;
68: private DataFlavor[] transferFlavors = NO_FLAVORS;
69: private DataContentHandler dataContentHandler;
70: private DataContentHandler factoryDCH;
71: private DataContentHandlerFactory oldFactory;
72: private String shortType;
73:
74:
78: public DataHandler(DataSource ds)
79: {
80: dataSource = ds;
81: oldFactory = factory;
82: }
83:
84:
89: public DataHandler(Object obj, String mimeType)
90: {
91: dataSource = null;
92: object = obj;
93: objectMimeType = mimeType;
94: oldFactory = factory;
95: }
96:
97:
101: public DataHandler(URL url)
102: {
103: dataSource = new URLDataSource(url);
104: oldFactory = factory;
105: }
106:
107:
110: public DataSource getDataSource()
111: {
112: if (dataSource != null)
113: {
114: return dataSource;
115: }
116: if (objDataSource == null)
117: {
118: objDataSource = new DataHandlerDataSource(this);
119: }
120: return objDataSource;
121: }
122:
123:
126: public String getName()
127: {
128: if (dataSource != null)
129: {
130: return dataSource.getName();
131: }
132: return null;
133: }
134:
135:
138: public String getContentType()
139: {
140: if (dataSource != null)
141: {
142: return dataSource.getContentType();
143: }
144: return objectMimeType;
145: }
146:
147:
150: public InputStream getInputStream()
151: throws IOException
152: {
153: if (dataSource != null)
154: {
155: return dataSource.getInputStream();
156: }
157: DataContentHandler dch = getDataContentHandler();
158: if (dch == null)
159: {
160: throw new UnsupportedDataTypeException("no DCH for MIME type " +
161: getShortType());
162: }
163: if ((dch instanceof ObjectDataContentHandler) &&
164: ((ObjectDataContentHandler)dch).getDCH() == null)
165: {
166: throw new UnsupportedDataTypeException("no object DCH " +
167: "for MIME type " +
168: getShortType());
169: }
170: PipedOutputStream pos = new PipedOutputStream();
171: DataContentHandlerWriter dchw =
172: new DataContentHandlerWriter(dch, object, objectMimeType, pos);
173: Thread thread = new Thread(dchw, "DataHandler.getInputStream");
174: thread.start();
175: return new PipedInputStream(pos);
176: }
177:
178: static class DataContentHandlerWriter
179: implements Runnable
180: {
181:
182: DataContentHandler dch;
183: Object object;
184: String mimeType;
185: OutputStream out;
186:
187: DataContentHandlerWriter(DataContentHandler dch, Object object,
188: String mimeType, OutputStream out)
189: {
190: this.dch = dch;
191: this.object = object;
192: this.mimeType = mimeType;
193: this.out = out;
194: }
195:
196: public void run()
197: {
198: try
199: {
200: dch.writeTo(object, mimeType, out);
201: }
202: catch(IOException e)
203: {
204: }
205: finally
206: {
207: try
208: {
209: out.close();
210: }
211: catch(IOException e)
212: {
213: }
214: }
215: }
216: }
217:
218:
222: public void writeTo(OutputStream os)
223: throws IOException
224: {
225: if (dataSource != null)
226: {
227: InputStream in = dataSource.getInputStream();
228: byte[] buf = new byte[8192];
229: for (int len = in.read(buf); len != -1; len = in.read(buf))
230: {
231: os.write(buf, 0, len);
232: }
233: in.close();
234: }
235: else
236: {
237: DataContentHandler dch = getDataContentHandler();
238: dch.writeTo(object, objectMimeType, os);
239: }
240: }
241:
242:
246: public OutputStream getOutputStream()
247: throws IOException
248: {
249: if (dataSource != null)
250: {
251: return dataSource.getOutputStream();
252: }
253: return null;
254: }
255:
256:
259: public synchronized DataFlavor[] getTransferDataFlavors()
260: {
261: if (factory != oldFactory || transferFlavors == NO_FLAVORS)
262: {
263: DataContentHandler dch = getDataContentHandler();
264: transferFlavors = dch.getTransferDataFlavors();
265: }
266: return transferFlavors;
267: }
268:
269:
273: public boolean isDataFlavorSupported(DataFlavor flavor)
274: {
275: DataFlavor[] flavors = getTransferDataFlavors();
276: for (int i = 0; i < flavors.length; i++)
277: {
278: if (flavors[i].equals(flavor))
279: {
280: return true;
281: }
282: }
283: return false;
284: }
285:
286:
290: public Object getTransferData(DataFlavor flavor)
291: throws UnsupportedFlavorException, IOException
292: {
293: DataContentHandler dch = getDataContentHandler();
294: return dch.getTransferData(flavor, dataSource);
295: }
296:
297:
302: public synchronized void setCommandMap(CommandMap commandMap)
303: {
304: if (commandMap != currentCommandMap || commandMap == null)
305: {
306: transferFlavors = NO_FLAVORS;
307: dataContentHandler = null;
308: currentCommandMap = commandMap;
309: }
310: }
311:
312:
315: public CommandInfo[] getPreferredCommands()
316: {
317: CommandMap commandMap = getCommandMap();
318: return commandMap.getPreferredCommands(getShortType());
319: }
320:
321:
324: public CommandInfo[] getAllCommands()
325: {
326: CommandMap commandMap = getCommandMap();
327: return commandMap.getAllCommands(getShortType());
328: }
329:
330:
334: public CommandInfo getCommand(String cmdName)
335: {
336: CommandMap commandMap = getCommandMap();
337: return commandMap.getCommand(getShortType(), cmdName);
338: }
339:
340:
343: public Object getContent()
344: throws IOException
345: {
346: DataContentHandler dch = getDataContentHandler();
347: return dch.getContent(getDataSource());
348: }
349:
350:
354: public Object getBean(CommandInfo cmdInfo)
355: {
356: try
357: {
358: return cmdInfo.getCommandObject(this, getClass().getClassLoader());
359: }
360: catch (IOException e)
361: {
362: e.printStackTrace(System.err);
363: return null;
364: }
365: catch (ClassNotFoundException e)
366: {
367: e.printStackTrace(System.err);
368: return null;
369: }
370: }
371:
372:
377: public static synchronized void
378: setDataContentHandlerFactory(DataContentHandlerFactory newFactory)
379: {
380: if (factory != null)
381: {
382: throw new Error("DataContentHandlerFactory already defined");
383: }
384: SecurityManager security = System.getSecurityManager();
385: if (security != null)
386: {
387: try
388: {
389: security.checkSetFactory();
390: }
391: catch (SecurityException e)
392: {
393: if (newFactory != null && DataHandler.class.getClassLoader()
394: != newFactory.getClass().getClassLoader())
395: {
396: throw e;
397: }
398: }
399: }
400: factory = newFactory;
401: }
402:
403:
407: private synchronized String getShortType()
408: {
409: if (shortType == null)
410: {
411: String contentType = getContentType();
412: try
413: {
414: MimeType mimeType = new MimeType(contentType);
415: shortType = mimeType.getBaseType();
416: }
417: catch (MimeTypeParseException e)
418: {
419: shortType = contentType;
420: }
421: }
422: return shortType;
423: }
424:
425:
428: private synchronized CommandMap getCommandMap()
429: {
430: if (currentCommandMap != null)
431: {
432: return currentCommandMap;
433: }
434: return CommandMap.getDefaultCommandMap();
435: }
436:
437:
440: private synchronized DataContentHandler getDataContentHandler()
441: {
442: if (factory != oldFactory)
443: {
444: oldFactory = factory;
445: factoryDCH = null;
446: dataContentHandler = null;
447: transferFlavors = NO_FLAVORS;
448: }
449: if (dataContentHandler != null)
450: {
451: return dataContentHandler;
452: }
453: String mimeType = getShortType();
454: if (factoryDCH == null && factory != null)
455: {
456: factoryDCH = factory.createDataContentHandler(mimeType);
457: }
458: if (factoryDCH != null)
459: {
460: dataContentHandler = factoryDCH;
461: }
462: if (dataContentHandler == null)
463: {
464: CommandMap commandMap = getCommandMap();
465: dataContentHandler = commandMap.createDataContentHandler(mimeType);
466: }
467: if (dataSource != null)
468: {
469: dataContentHandler =
470: new DataSourceDataContentHandler(dataContentHandler, dataSource);
471: }
472: else
473: {
474: dataContentHandler =
475: new ObjectDataContentHandler(dataContentHandler, object,
476: objectMimeType);
477: }
478: return dataContentHandler;
479: }
480:
481: }