From 94ae08701bbd7585a0b7e5a92d1975965a503c03 Mon Sep 17 00:00:00 2001
From: Dennis Kao <ulysseskao@gmail.com>
Date: Wed, 15 Jan 2014 11:28:52 +0800
Subject: [PATCH] Merge branch 'origin/2.1.x'

---
 xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleElementLogger.java |  265 ++++++++++++++++++----------------------------------
 1 files changed, 94 insertions(+), 171 deletions(-)

diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleElementLogger.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleElementLogger.java
index 8615537..542d36e 100644
--- a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleElementLogger.java
+++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleElementLogger.java
@@ -25,18 +25,17 @@
 import com.ximple.io.dgn7.ArcElement;
 import com.ximple.io.dgn7.ComplexChainElement;
 import com.ximple.io.dgn7.ComplexShapeElement;
+import com.ximple.io.dgn7.Dgn7fileException;
 import com.ximple.io.dgn7.Element;
 import com.ximple.io.dgn7.EllipseElement;
+import com.ximple.io.dgn7.IElementHandler;
 import com.ximple.io.dgn7.LineElement;
 import com.ximple.io.dgn7.LineStringElement;
 import com.ximple.io.dgn7.ShapeElement;
 import com.ximple.io.dgn7.TextElement;
 import com.ximple.io.dgn7.TextNodeElement;
-import com.ximple.io.dgn7.IElementHandler;
-import com.ximple.io.dgn7.Dgn7fileException;
 
-public class OracleElementLogger
-{
+public class OracleElementLogger {
     static Log logger = LogFactory.getLog(OracleElementLogger.class);
     private static final String DEFAULT_ELMOUTPATH = "elmout";
     private static final String TAB_IGDSSEED = "SD$IGDSSET_SEED";
@@ -56,42 +55,34 @@
     private String elmOutPath;
     private String prefix = null;
 
-    public OracleElementLogger(Connection connection)
-    {
+    public OracleElementLogger(Connection connection) {
         this.connection = connection;
         elmOutPath = DEFAULT_ELMOUTPATH;
     }
 
-    public OracleElementLogger(Connection connection, String elmOutPath)
-    {
+    public OracleElementLogger(Connection connection, String elmOutPath) {
         this.connection = connection;
         this.elmOutPath = elmOutPath;
     }
 
-    public OracleElementLogger(Connection connection, int maxCount)
-    {
+    public OracleElementLogger(Connection connection, int maxCount) {
         this.connection = connection;
         elmOutPath = DEFAULT_ELMOUTPATH;
         this.maxElmCount = maxCount;
     }
 
-    public OracleElementLogger(Connection connection, String elmOutPath, int maxCount)
-    {
+    public OracleElementLogger(Connection connection, String elmOutPath, int maxCount) {
         this.connection = connection;
         this.elmOutPath = elmOutPath;
         this.maxElmCount = maxCount;
     }
-    
-    public String getDataOutPath()
-    {
-        if (dataOut == null)
-        {
+
+    public String getDataOutPath() {
+        if (dataOut == null) {
             File outPath = new File(getDataPath(), elmOutPath);
-            if (!outPath.exists())
-            {
+            if (!outPath.exists()) {
                 outPath.mkdir();
-            } else if (!outPath.isDirectory())
-            {
+            } else if (!outPath.isDirectory()) {
                 outPath.mkdir();
             }
             dataOut = outPath.toString();
@@ -99,48 +90,36 @@
         return dataOut;
     }
 
-    public String getDataPath()
-    {
+    public String getDataPath() {
         return dataPath;
     }
 
-    public void setDataPath(String dataPath)
-    {
+    public void setDataPath(String dataPath) {
         this.dataPath = dataPath;
     }
 
-    public void logElement(Element element, String currentSchema)
-    {
+    public void logElement(Element element, String currentSchema) {
         if ((this.currentSchema == null) ||
-                (!this.currentSchema.equalsIgnoreCase(currentSchema)))
-        {
+            (!this.currentSchema.equalsIgnoreCase(currentSchema))) {
             schemaChanged = true;
             this.currentSchema = currentSchema;
-            try
-            {
+            try {
                 createNewStream();
-            } catch (IOException e)
-            {
+            } catch (IOException e) {
                 logger.warn(e.getMessage(), e);
                 return;
-            } catch (SQLException e)
-            {
+            } catch (SQLException e) {
                 logger.warn(e.getMessage(), e);
                 return;
             }
-        } else
-        {
-            if (fch == null)
-            {
-                try
-                {
+        } else {
+            if (fch == null) {
+                try {
                     createNewStream();
-                } catch (IOException e)
-                {
+                } catch (IOException e) {
                     logger.warn(e.getMessage(), e);
                     return;
-                } catch (SQLException e)
-                {
+                } catch (SQLException e) {
                     logger.warn(e.getMessage(), e);
                     return;
                 }
@@ -148,151 +127,121 @@
         }
 
         ArrayList<ByteBuffer> subBuffers = new ArrayList<ByteBuffer>();
-        if (fch != null)
-        {
+        if (fch != null) {
             ByteBuffer buf = null;
-            if (element instanceof LineElement)
-            {
+            if (element instanceof LineElement) {
                 int size = LineElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 LineElement.ElementHandler.getInstance().write(buf, element);
-            } else if (element instanceof ShapeElement)
-            {
+            } else if (element instanceof ShapeElement) {
                 int size = ShapeElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 ShapeElement.ElementHandler.getInstance().write(buf, element);
-            } else if (element instanceof LineStringElement)
-            {
+            } else if (element instanceof LineStringElement) {
                 int size = LineStringElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 LineStringElement.ElementHandler.getInstance().write(buf, element);
-            } else if (element instanceof ComplexChainElement)
-            {
+            } else if (element instanceof ComplexChainElement) {
                 int size = ComplexChainElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 ComplexChainElement.ElementHandler.getInstance().write(buf, element);
                 ComplexChainElement complexElement = (ComplexChainElement) element;
                 ListIterator it = complexElement.listIterator();
-                while (it.hasNext())
-                {
+                while (it.hasNext()) {
                     Element subElm = (Element) it.next();
-                    try
-                    {
+                    try {
                         IElementHandler handler = subElm.getElementType().getElementHandler();
                         size = handler.getLength(subElm);
                         ByteBuffer subBuf = ByteBuffer.allocate(size * 2);
                         handler.write(subBuf, subElm);
                         subBuffers.add(subBuf);
-                    } catch (Dgn7fileException e)
-                    {
+                    } catch (Dgn7fileException e) {
                         logger.warn(e.getMessage(), e);
                     }
                 }
-            } else if (element instanceof ComplexShapeElement)
-            {
+            } else if (element instanceof ComplexShapeElement) {
                 int size = ComplexShapeElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 ComplexShapeElement.ElementHandler.getInstance().write(buf, element);
                 ComplexShapeElement complexElement = (ComplexShapeElement) element;
                 ListIterator it = complexElement.listIterator();
-                while (it.hasNext())
-                {
+                while (it.hasNext()) {
                     Element subElm = (Element) it.next();
-                    try
-                    {
+                    try {
                         IElementHandler handler = subElm.getElementType().getElementHandler();
                         size = handler.getLength(subElm);
                         ByteBuffer subBuf = ByteBuffer.allocate(size * 2);
                         handler.write(subBuf, subElm);
                         subBuffers.add(subBuf);
-                    } catch (Dgn7fileException e)
-                    {
+                    } catch (Dgn7fileException e) {
                         logger.warn(e.getMessage(), e);
                     }
                 }
-            } else if (element instanceof ArcElement)
-            {
+            } else if (element instanceof ArcElement) {
                 int size = ArcElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 ArcElement.ElementHandler.getInstance().write(buf, element);
-            } else if (element instanceof EllipseElement)
-            {
+            } else if (element instanceof EllipseElement) {
                 int size = EllipseElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 EllipseElement.ElementHandler.getInstance().write(buf, element);
-            } else if (element instanceof TextElement)
-            {
+            } else if (element instanceof TextElement) {
                 int size = TextElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 TextElement.ElementHandler.getInstance().write(buf, element);
-            } else if (element instanceof TextNodeElement)
-            {
+            } else if (element instanceof TextNodeElement) {
                 int size = TextNodeElement.ElementHandler.getInstance().getLength(element);
                 buf = ByteBuffer.allocate(size * 2);
                 TextNodeElement.ElementHandler.getInstance().write(buf, element);
                 TextNodeElement nodeElement = (TextNodeElement) element;
                 ListIterator it = nodeElement.listIterator();
-                while (it.hasNext())
-                {
+                while (it.hasNext()) {
                     Element subElm = (Element) it.next();
-                    try
-                    {
+                    try {
                         IElementHandler handler = subElm.getElementType().getElementHandler();
                         size = handler.getLength(subElm);
                         ByteBuffer subBuf = ByteBuffer.allocate(size * 2);
                         handler.write(subBuf, subElm);
                         subBuffers.add(subBuf);
-                    } catch (Dgn7fileException e)
-                    {
+                    } catch (Dgn7fileException e) {
                         logger.warn(e.getMessage(), e);
                     }
                 }
             }
 
-            if ((buf != null) && (fch != null))
-            {
-                try
-                {
+            if ((buf != null) && (fch != null)) {
+                try {
                     buf.position(0);
                     int size = fch.write(buf);
-                    if (size != buf.limit())
-                    {
+                    if (size != buf.limit()) {
                         long position = fch.position();
                         logger.info("Pos:" + position);
                     }
-                } catch (IOException e)
-                {
+                } catch (IOException e) {
                     logger.warn(e.getMessage(), e);
                 }
             }
         }
 
         elmCount++;
-        if ((subBuffers.size() != 0) && (fch != null))
-        {
-            for (ByteBuffer buf : subBuffers)
-            {
-                try
-                {
+        if ((subBuffers.size() != 0) && (fch != null)) {
+            for (ByteBuffer buf : subBuffers) {
+                try {
                     buf.position(0);
                     int size = fch.write(buf);
-                    if (size != buf.limit())
-                    {
+                    if (size != buf.limit()) {
                         long position = fch.position();
                         logger.info("Pos:" + position);
                     }
-                } catch (IOException e)
-                {
+                } catch (IOException e) {
                     logger.warn(e.getMessage(), e);
                 }
             }
         }
     }
 
-    private void createNewStream() throws IOException, SQLException
-    {
-        if (fos != null)
-        {
+    private void createNewStream() throws IOException, SQLException {
+        if (fos != null) {
             putEndOfFileElement();
             fos.close();
             fos = null;
@@ -300,16 +249,13 @@
         }
 
         String outLogName = currentSchema + ".dgn";
-        if (prefix != null)
-        {
+        if (prefix != null) {
             outLogName = prefix + outLogName;
         }
         File logFile = new File(getDataOutPath(), outLogName);
-        while (logFile.exists())
-        {
+        while (logFile.exists()) {
             outLogName = this.currentSchema + "-" + (++logCount) + ".dgn";
-            if (prefix != null)
-            {
+            if (prefix != null) {
                 outLogName = prefix + outLogName;
             }
 
@@ -324,8 +270,7 @@
         elmCount = 0;
     }
 
-    private void putEndOfFileElement() throws IOException
-    {
+    private void putEndOfFileElement() throws IOException {
         if (fch == null)
             return;
         ByteBuffer bf = ByteBuffer.allocate(4);
@@ -333,18 +278,14 @@
         fch.write(bf);
     }
 
-    private void prepareOutputElementStream() throws SQLException, IOException
-    {
-        if (connection == null)
-        {
+    private void prepareOutputElementStream() throws SQLException, IOException {
+        if (connection == null) {
             logger.warn("connection is null");
             return;
         }
 
-        if (dgnFileHeader != null)
-        {
-            for (byte[] raw : dgnFileHeader)
-            {
+        if (dgnFileHeader != null) {
+            for (byte[] raw : dgnFileHeader) {
                 putElementIntoStream(raw);
             }
             return;
@@ -358,23 +299,19 @@
         ResultSet rsSrc = stmtSrc.executeQuery(fetchSrcStmt);
         int igdsMetaType = rsSrc.getMetaData().getColumnType(1);
 
-        while (rsSrc.next())
-        {
+        while (rsSrc.next()) {
             byte[] raw;
 
-            if (igdsMetaType == Types.BLOB)
-            {
+            if (igdsMetaType == Types.BLOB) {
                 BLOB blob = (BLOB) rsSrc.getBlob(1);
 
                 raw = getBytesFromBLOB(blob);
-                blob.close();
-            } else
-            {
+                // blob.close();
+            } else {
                 raw = rsSrc.getBytes(1);
             }
 
-            if (raw != null)
-            {
+            if (raw != null) {
                 dgnFileHeader.add(raw);
                 putElementIntoStream(raw);
             }
@@ -383,14 +320,12 @@
         stmtSrc.close();
     }
 
-    private void putElementIntoStream(byte[] raw) throws IOException
-    {
+    private void putElementIntoStream(byte[] raw) throws IOException {
         if (fch != null)
             fch.write(ByteBuffer.wrap(raw));
     }
 
-    protected static byte[] getBytesFromBLOB(BLOB blob) throws SQLException
-    {
+    protected byte[] getBytesFromBLOB(BLOB blob) throws SQLException {
         byte[] raw = null;
 
         int optimalSize = blob.getChunkSize();
@@ -399,48 +334,44 @@
         ByteBuffer buffer = null;    // ByteBuffer.allocate(optimalSize);
         int len;
 
-        try
-        {
-            while ((len = (is.read(chunk))) != -1)
-            {
-                if (buffer != null)
-                {
+        try {
+            while ((len = (is.read(chunk))) != -1) {
+                if (buffer != null) {
                     buffer.limit(buffer.limit() + len);
-                } else
-                {
+                } else {
                     buffer = ByteBuffer.allocate(len);
                 }
 
                 buffer.put(chunk);
             }
 
-            is.close();
             assert buffer != null;
             buffer.position(0);
             raw = buffer.array();
-        } catch (IOException e)
-        {
+        } catch (IOException e) {
             logger.warn(e.getMessage(), e);
             Assert.shouldNeverReachHere();
+        } finally {
+            try {
+                is.close();
+            } catch (IOException e) {
+                logger.warn("InputStream cannot close", e);
+            }
+            ;
         }
         return raw;
     }
 
-    public void flashLogging()
-    {
-        if ((useElementCount) && (elmCount < maxElmCount))
-        {
+    public void flashLogging() {
+        if ((useElementCount) && (elmCount < maxElmCount)) {
             return;
         }
 
-        if (fos != null)
-        {
-            try
-            {
+        if (fos != null) {
+            try {
                 putEndOfFileElement();
                 fos.close();
-            } catch (IOException e)
-            {
+            } catch (IOException e) {
                 logger.warn(e.getMessage(), e);
             }
             fos = null;
@@ -449,43 +380,35 @@
         }
     }
 
-    public boolean isSchemaChanged()
-    {
+    public boolean isSchemaChanged() {
         return schemaChanged;
     }
 
-    public boolean isUseElementCount()
-    {
+    public boolean isUseElementCount() {
         return useElementCount;
     }
 
-    public void setUseElementCount(boolean useElementCount)
-    {
+    public void setUseElementCount(boolean useElementCount) {
         this.useElementCount = useElementCount;
     }
 
-    public int getElmCount()
-    {
+    public int getElmCount() {
         return elmCount;
     }
 
-    public int getMaxElmCount()
-    {
+    public int getMaxElmCount() {
         return maxElmCount;
     }
 
-    public void setMaxElmCount(int maxElmCount)
-    {
+    public void setMaxElmCount(int maxElmCount) {
         this.maxElmCount = maxElmCount;
     }
 
-    public String getLogPrefix()
-    {
+    public String getLogPrefix() {
         return prefix;
     }
 
-    public void setLogPrefix(String prefix)
-    {
+    public void setLogPrefix(String prefix) {
         this.prefix = prefix;
     }
 }

--
Gitblit v0.0.0-SNAPSHOT