PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/carrot-jdk7-jnlp-decompile-deploy/src/main/java/com/sun/deploy/net/socket/UnixDomainSocket.java

https://github.com/carrot-garden/carrot-jnlper
Java | 458 lines | 413 code | 41 blank | 4 comment | 60 complexity | d45a3c1bfec2a50ac9c9d319dc9f50f5 MD5 | raw file
  1. package com.sun.deploy.net.socket;
  2. import com.sun.deploy.config.Platform;
  3. import java.io.File;
  4. import java.nio.BufferOverflowException;
  5. import java.nio.BufferUnderflowException;
  6. import java.nio.ByteBuffer;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. public class UnixDomainSocket
  11. {
  12. public static final String pipeFileNamePrefix = ".com.sun.deploy.net.socket.";
  13. public static final String pipeFileNameSuffix = ".AF_UNIX";
  14. public static final int STATUS_CLOSE = 0;
  15. public static final int STATUS_OPEN = 1;
  16. public static final int STATUS_CONNECT = 2;
  17. public static final int STATUS_BIND = 3;
  18. public static final int STATUS_LISTEN = 4;
  19. public static final int STATUS_ACCEPT = 5;
  20. private volatile int socketStatus;
  21. private volatile long socketHandle;
  22. private volatile boolean unlinkFile;
  23. private String fileName;
  24. private boolean abstractNamespace;
  25. private int protocol;
  26. private int backlog;
  27. private static ShutdownHookUnlinkFiles shutdownHookUnlinkFiles;
  28. public static boolean isSupported()
  29. {
  30. return UnixSocketImpl.unStreamSocketSupported();
  31. }
  32. public static UnixDomainSocket CreateClientConnect(String paramString, boolean paramBoolean, int paramInt)
  33. throws UnixDomainSocketException, UnsupportedOperationException
  34. {
  35. UnixDomainSocket localUnixDomainSocket = new UnixDomainSocket(paramString, paramBoolean, paramInt);
  36. localUnixDomainSocket.connect();
  37. return localUnixDomainSocket;
  38. }
  39. public static UnixDomainSocket CreateServerBindListen(String paramString, boolean paramBoolean, int paramInt1, int paramInt2)
  40. throws UnixDomainSocketException, UnsupportedOperationException
  41. {
  42. UnixDomainSocket localUnixDomainSocket = new UnixDomainSocket(paramString, paramBoolean, paramInt1);
  43. localUnixDomainSocket.bind();
  44. localUnixDomainSocket.listen(paramInt2);
  45. return localUnixDomainSocket;
  46. }
  47. public static UnixDomainSocket CreateServerBindListen(int paramInt1, int paramInt2)
  48. throws UnixDomainSocketException, UnsupportedOperationException
  49. {
  50. UnixDomainSocket localUnixDomainSocket = new UnixDomainSocket(paramInt1);
  51. localUnixDomainSocket.bind();
  52. localUnixDomainSocket.listen(paramInt2);
  53. return localUnixDomainSocket;
  54. }
  55. public UnixDomainSocket(String paramString, boolean paramBoolean, int paramInt)
  56. throws UnixDomainSocketException, UnsupportedOperationException
  57. {
  58. setup(paramString, false, paramInt);
  59. }
  60. public UnixDomainSocket(int paramInt)
  61. throws UnixDomainSocketException, UnsupportedOperationException
  62. {
  63. long l = Platform.get().getNativePID();
  64. String str = null;
  65. try
  66. {
  67. File localFile1 = localFile1 = File.createTempFile(".com.sun.deploy.net.socket." + String.valueOf(l) + ".", ".AF_UNIX");
  68. str = localFile1.getAbsolutePath();
  69. if (localFile1.exists())
  70. localFile1.delete();
  71. else
  72. str = null;
  73. }
  74. catch (Exception localException1)
  75. {
  76. localException1.printStackTrace();
  77. str = null;
  78. }
  79. if (str == null)
  80. try
  81. {
  82. str = new String("/tmp/.com.sun.deploy.net.socket." + String.valueOf(l) + ".AF_UNIX");
  83. File localFile2 = localFile2 = new File(str);
  84. if (localFile2.exists())
  85. localFile2.delete();
  86. }
  87. catch (Exception localException2)
  88. {
  89. localException2.printStackTrace();
  90. str = null;
  91. }
  92. if (str == null)
  93. throw new RuntimeException("could not create a temp pipe filename");
  94. setup(str, false, paramInt);
  95. }
  96. public synchronized void open()
  97. throws UnixDomainSocketException, UnsupportedOperationException
  98. {
  99. validateSocketStatusTransition(1);
  100. this.socketHandle = UnixSocketImpl.unStreamSocketCreate(this.fileName, this.abstractNamespace, this.protocol);
  101. this.socketStatus = 1;
  102. }
  103. public void close()
  104. {
  105. this.socketStatus = 0;
  106. if (0L != this.socketHandle)
  107. {
  108. long l = this.socketHandle;
  109. this.socketHandle = 0L;
  110. try
  111. {
  112. UnixSocketImpl.unStreamSocketClose(l);
  113. }
  114. catch (Exception localException2)
  115. {
  116. }
  117. }
  118. if (this.unlinkFile)
  119. try
  120. {
  121. File localFile = new File(this.fileName);
  122. if (null != localFile)
  123. localFile.delete();
  124. }
  125. catch (Exception localException1)
  126. {
  127. }
  128. this.backlog = 0;
  129. }
  130. public void deleteFileOnClose()
  131. {
  132. if ((!this.unlinkFile) && (!this.abstractNamespace))
  133. {
  134. this.unlinkFile = true;
  135. shutdownHookUnlinkFiles.add(this.fileName);
  136. }
  137. }
  138. public synchronized void bind()
  139. throws UnixDomainSocketException, UnsupportedOperationException
  140. {
  141. validateSocketStatusTransition(3);
  142. UnixSocketImpl.unStreamSocketBind(this.socketHandle);
  143. this.socketStatus = 3;
  144. deleteFileOnClose();
  145. }
  146. public synchronized void listen(int paramInt)
  147. throws UnixDomainSocketException, UnsupportedOperationException
  148. {
  149. validateSocketStatusTransition(4);
  150. UnixSocketImpl.unStreamSocketListen(this.socketHandle, paramInt);
  151. this.backlog = paramInt;
  152. this.socketStatus = 4;
  153. }
  154. public UnixDomainSocket accept()
  155. throws UnixDomainSocketException, UnsupportedOperationException
  156. {
  157. validateSocketStatusTransition(5);
  158. long l = UnixSocketImpl.unStreamSocketAccept(this.socketHandle);
  159. this.socketStatus = 5;
  160. return new UnixDomainSocket(this, l, 2);
  161. }
  162. public void connect()
  163. throws UnixDomainSocketException, UnsupportedOperationException
  164. {
  165. validateSocketStatusTransition(2);
  166. UnixSocketImpl.unStreamSocketConnect(this.socketHandle);
  167. this.socketStatus = 2;
  168. }
  169. public int read(ByteBuffer paramByteBuffer)
  170. throws UnixDomainSocketException, BufferOverflowException, UnsupportedOperationException
  171. {
  172. return read(paramByteBuffer, paramByteBuffer.remaining());
  173. }
  174. public int read(ByteBuffer paramByteBuffer, int paramInt)
  175. throws UnixDomainSocketException, BufferOverflowException, UnsupportedOperationException
  176. {
  177. validateSocketStatusForReadWrite();
  178. if (null == paramByteBuffer)
  179. throw new IllegalArgumentException("Argument buffer is null");
  180. if (!paramByteBuffer.isDirect())
  181. throw new IllegalArgumentException("Argument buffer is not direct");
  182. int i = paramByteBuffer.limit();
  183. int j = paramByteBuffer.position();
  184. if (j >= i)
  185. throw new BufferOverflowException();
  186. if (j + paramInt > i)
  187. paramInt = i - j;
  188. int k = UnixSocketImpl.unStreamSocketRead(this.socketHandle, paramByteBuffer, j, paramInt);
  189. if (k > 0)
  190. paramByteBuffer.position(j + k);
  191. return k;
  192. }
  193. public int write(ByteBuffer paramByteBuffer)
  194. throws UnixDomainSocketException, BufferUnderflowException, UnsupportedOperationException
  195. {
  196. return write(paramByteBuffer, paramByteBuffer.remaining());
  197. }
  198. public int write(ByteBuffer paramByteBuffer, int paramInt)
  199. throws UnixDomainSocketException, BufferUnderflowException, UnsupportedOperationException
  200. {
  201. validateSocketStatusForReadWrite();
  202. if (null == paramByteBuffer)
  203. throw new IllegalArgumentException("Argument buffer is null");
  204. if (!paramByteBuffer.isDirect())
  205. throw new IllegalArgumentException("Argument buffer is not direct");
  206. int i = paramByteBuffer.limit();
  207. int j = paramByteBuffer.position();
  208. if (j >= i)
  209. throw new BufferUnderflowException();
  210. if (j + paramInt > i)
  211. paramInt = i - j;
  212. int k = UnixSocketImpl.unStreamSocketWrite(this.socketHandle, paramByteBuffer, j, paramInt);
  213. if (k > 0)
  214. paramByteBuffer.position(j + k);
  215. return k;
  216. }
  217. public synchronized boolean isOpenAndValid()
  218. throws UnsupportedOperationException
  219. {
  220. boolean bool = false;
  221. if ((0L != this.socketHandle) && (this.socketStatus != 0))
  222. try
  223. {
  224. bool = UnixSocketImpl.unStreamSocketIsValid(this.socketHandle);
  225. }
  226. catch (Exception localException)
  227. {
  228. }
  229. if (!bool)
  230. this.socketStatus = 0;
  231. return bool;
  232. }
  233. public boolean isOpen()
  234. {
  235. return this.socketStatus != 0;
  236. }
  237. public boolean isConnected()
  238. {
  239. return this.socketStatus != 2;
  240. }
  241. public String getFilename()
  242. {
  243. return this.fileName;
  244. }
  245. public boolean getIsAbstractNamespace()
  246. {
  247. return this.abstractNamespace;
  248. }
  249. public int getProtocol()
  250. {
  251. return this.protocol;
  252. }
  253. public int getBacklog()
  254. {
  255. return this.backlog;
  256. }
  257. public int getStatus()
  258. {
  259. return this.socketStatus;
  260. }
  261. public String getStatusAsString()
  262. {
  263. switch (this.socketStatus)
  264. {
  265. case 0:
  266. return "close";
  267. case 1:
  268. return "open";
  269. case 2:
  270. return "connect";
  271. case 3:
  272. return "bind";
  273. case 4:
  274. return "listen";
  275. case 5:
  276. return "accept";
  277. }
  278. return "invalid";
  279. }
  280. public boolean isServer()
  281. {
  282. switch (this.socketStatus)
  283. {
  284. case 3:
  285. case 4:
  286. case 5:
  287. return true;
  288. }
  289. return false;
  290. }
  291. public String getNativeInfo()
  292. {
  293. String str = "n.a.";
  294. if (0L != this.socketHandle)
  295. try
  296. {
  297. str = UnixSocketImpl.unStreamSocketGetNativeInfo(this.socketHandle);
  298. }
  299. catch (Exception localException)
  300. {
  301. }
  302. return str;
  303. }
  304. public String toString()
  305. {
  306. return "UnixDomainSocket[" + getStatusAsString() + ", pipe: " + getFilename() + ", ans: " + getIsAbstractNamespace() + ", proto: " + getProtocol() + ", backlog: " + getBacklog() + ", info: " + getNativeInfo() + "]";
  307. }
  308. protected void finalize()
  309. throws Throwable
  310. {
  311. try
  312. {
  313. close();
  314. }
  315. finally
  316. {
  317. super.finalize();
  318. }
  319. }
  320. private void setup(String paramString, boolean paramBoolean, int paramInt)
  321. throws UnixDomainSocketException, UnsupportedOperationException
  322. {
  323. if (null == paramString)
  324. throw new IllegalArgumentException("Argument fileName is null");
  325. this.socketHandle = 0L;
  326. this.fileName = paramString;
  327. this.abstractNamespace = paramBoolean;
  328. this.protocol = paramInt;
  329. this.backlog = 0;
  330. this.socketStatus = 0;
  331. open();
  332. }
  333. private UnixDomainSocket(UnixDomainSocket paramUnixDomainSocket, long paramLong, int paramInt)
  334. {
  335. this.socketHandle = paramLong;
  336. this.fileName = paramUnixDomainSocket.fileName;
  337. this.abstractNamespace = paramUnixDomainSocket.abstractNamespace;
  338. this.protocol = paramUnixDomainSocket.protocol;
  339. this.backlog = 0;
  340. this.socketStatus = paramInt;
  341. }
  342. private void validateSocketStatusTransition(int paramInt)
  343. throws UnixDomainSocketException
  344. {
  345. if ((0L == this.socketHandle) && (0 != this.socketStatus))
  346. throw new UnixDomainSocketException(toString(), UnixSocketException.EINVAL);
  347. switch (paramInt)
  348. {
  349. case 1:
  350. if (this.socketStatus != 0)
  351. break;
  352. return;
  353. case 2:
  354. case 3:
  355. if (this.socketStatus != 1)
  356. break;
  357. return;
  358. case 4:
  359. if (this.socketStatus != 3)
  360. break;
  361. return;
  362. case 5:
  363. if ((this.socketStatus != 4) && (this.socketStatus != 5))
  364. break;
  365. return;
  366. }
  367. throw new UnixDomainSocketException(toString(), UnixSocketException.EINVAL);
  368. }
  369. private void validateSocketStatusForReadWrite()
  370. throws UnixDomainSocketException
  371. {
  372. if ((0L != this.socketHandle) && (2 == this.socketStatus))
  373. return;
  374. throw new UnixDomainSocketException(toString(), UnixSocketException.EINVAL);
  375. }
  376. static
  377. {
  378. Platform.get();
  379. shutdownHookUnlinkFiles = new ShutdownHookUnlinkFiles();
  380. Runtime.getRuntime().addShutdownHook(shutdownHookUnlinkFiles);
  381. }
  382. private static class ShutdownHookUnlinkFiles extends Thread
  383. {
  384. private List files = new ArrayList();
  385. public synchronized void add(String paramString)
  386. {
  387. try
  388. {
  389. File localFile = new File(paramString);
  390. this.files.add(localFile);
  391. }
  392. catch (Exception localException)
  393. {
  394. }
  395. }
  396. public void run()
  397. {
  398. Iterator localIterator = this.files.iterator();
  399. while (localIterator.hasNext())
  400. {
  401. File localFile = (File)localIterator.next();
  402. try
  403. {
  404. if (null != localFile)
  405. localFile.delete();
  406. }
  407. catch (Exception localException)
  408. {
  409. }
  410. }
  411. }
  412. }
  413. }
  414. /* Location: /opt/sun/java32/jdk1.7.0_04/jre/lib/deploy.jar
  415. * Qualified Name: com.sun.deploy.net.socket.UnixDomainSocket
  416. * JD-Core Version: 0.6.0
  417. */