Source Code Cross Referenced for MyOodbDatabase.java in  » Database-DBMS » myoodb-2.2.1 » org » myoodb » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » myoodb 2.2.1 » org.myoodb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:///////////////////////////////////////////////////////////////////////////////
002://
003://   Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004://
005://                          All Rights Reserved
006://
007://   This program is free software; you can redistribute it and/or modify
008://   it under the terms of the GNU General Public License and GNU Library
009://   General Public License as published by the Free Software Foundation;
010://   either version 2, or (at your option) any later version.
011://
012://   This program is distributed in the hope that it will be useful,
013://   but WITHOUT ANY WARRANTY; without even the implied warranty of
014://   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015://   GNU General Public License and GNU Library General Public License
016://   for more details.
017://
018://   You should have received a copy of the GNU General Public License
019://   and GNU Library General Public License along with this program; if
020://   not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021://   MA 02139, USA.
022://
023:///////////////////////////////////////////////////////////////////////////////
024:package org.myoodb;
025:
026:import javax.net.ssl.*;
027:
028:import org.myoodb.core.AbstractConnection;
029:import org.myoodb.core.command.AbstractCommand;
030:import org.myoodb.core.command.TransactionCommand;
031:
032:public final class MyOodbDatabase extends org.myoodb.core.AbstractDatabase
033:{
034:    private static final org.myoodb.util.Logger LOGGER = org.myoodb.util.Logger.getLogger(MyOodbDatabase.class);
035:
036:    private final static String PROP_HOST = "host";
037:    private final static String PROP_PORT = "port";
038:    private final static String PROP_PROTOCOL = "protocol";
039:    private final static String PROP_USERNAME = "username";
040:    private final static String PROP_PASSWORD = "password";
041:    private final static String PROP_PASSPHRASE = "passPhrase";
042:
043:    private final static String PROP_CONNECT_TIMEOUT = "connectTimeout";
044:    private final static String PROP_CONTEXT = "context";
045:
046:    private final static java.util.HashSet s_databases = new java.util.HashSet();
047:
048:    private static volatile long m_connectIdentifier = 0;
049:    private static boolean s_gameConfigurationFlag = @gameConfiguration@;
050:    private static boolean s_tunnelPassThroughFlag = @tunnelPassThrough@ || @gameConfiguration@;
051:
052:    private int m_port;
053:    private String m_host;
054:    private String m_username;
055:    private String m_password;
056:    private String m_protocol;
057:    private String m_passPhrase;
058:
059:    private Object m_context;
060:    private int m_connectTimeout = -1;
061:
062:    private java.util.HashMap m_txTable;
063:    private java.util.HashSet m_usedConnections;
064:    private java.util.TreeSet m_availableConnections;
065:
066:    public static MyOodbDatabase open(String uriString, String username, String password) throws Exception
067:    {
068:        return open(uriString, username, password, null);
069:    }
070:
071:    public static MyOodbDatabase open(String uriString, String username, String password, String passPhrase) throws Exception
072:    {
073:        return open(uriString, username, password, passPhrase, -1, (Object) null);
074:    }
075:
076:    public static MyOodbDatabase open(String uriString, String username, String password, String passPhrase, int connectTimeout) throws Exception
077:    {
078:        return open(uriString, username, password, passPhrase, connectTimeout, (Object) null);
079:    }
080:
081:    public static MyOodbDatabase open(String uriString, String username, String password, String passPhrase, int connectTimeout, String tunnelExtension) throws Exception
082:    {
083:        return open(uriString, username, password, passPhrase, connectTimeout, (Object) tunnelExtension);
084:    }
085:
086:    public static MyOodbDatabase open(String uriString, String username, String password, String passPhrase, int connectTimeout, SSLContext sslContext) throws Exception
087:    {
088:        return open(uriString, username, password, passPhrase, connectTimeout, (Object) sslContext);
089:    }
090:
091:    public static MyOodbDatabase open(String uriString, String username, String password, String passPhrase, int connectTimeout, Object context) throws Exception
092:    {
093:        MyOodbDatabase db = new MyOodbDatabase();
094:
095:        try
096:        {
097:            java.util.HashMap props = createProperties(uriString);
098:            props.put(PROP_USERNAME, username);
099:            props.put(PROP_PASSWORD, password);
100:            props.put(PROP_PASSPHRASE, passPhrase);
101:
102:            props.put(PROP_CONNECT_TIMEOUT, connectTimeout);
103:            props.put(PROP_CONTEXT, context);
104:
105:            db.open(props);
106:        }
107:        catch (Exception e)
108:        {
109:            db.close();
110:            throw e;
111:        }
112:
113:        return db;
114:    }
115:
116:    public static void setGameConfigurationFlag(boolean flag)
117:    {
118:        s_gameConfigurationFlag = flag;
119:
120:        setTunnelPassThroughFlag(flag);
121:
122:        org.myoodb.core.FileHelper.setMemoryResidentFlag(flag);
123:    }
124:
125:    public static boolean getGameConfigurationFlag()
126:    {
127:        return s_gameConfigurationFlag;
128:    }
129:
130:    public static void setTunnelPassThroughFlag(boolean flag)
131:    {
132:        s_tunnelPassThroughFlag = flag;
133:    }
134:
135:    public static boolean getTunnelPassThroughFlag()
136:    {
137:        return s_tunnelPassThroughFlag;
138:    }
139:
140:    public static void close(AbstractConnection connection)
141:    {
142:        if (connection.isConnected() == true)
143:        {
144:            try
145:            {
146:                connection.send(new org.myoodb.core.command.LogoutCommand());
147:            }
148:            catch (java.io.IOException e)
149:            {
150:                // nothing to do
151:            }
152:            catch (Exception e)
153:            {
154:                LOGGER.error(null, e);
155:            }
156:
157:            try
158:            {
159:                connection.close();
160:            }
161:            catch (java.io.IOException e)
162:            {
163:                // nothing to do
164:            }
165:            catch (Exception e)
166:            {
167:                LOGGER.error(null, e);
168:            }
169:        }
170:    }
171:
172:    private MyOodbDatabase()
173:    {
174:        m_port = -1;
175:        m_host = null;
176:        m_username = null;
177:        m_password = null;
178:        m_protocol = null;
179:
180:        m_txTable = null;
181:        m_usedConnections = null;
182:        m_availableConnections = null;
183:    }
184:
185:    private MyOodbTransaction getTransactionByThread(Thread thread)
186:    {
187:        MyOodbTransaction result = null;
188:
189:        java.util.LinkedList txList = (java.util.LinkedList) m_txTable.get(thread);
190:        if (txList != null)
191:        {
192:            result = (MyOodbTransaction) txList.getLast();
193:        }
194:
195:        return result;
196:    }
197:
198:    private java.util.LinkedList getThreadListByThread(Thread thread)
199:    {
200:        return (java.util.LinkedList) m_txTable.get(thread);
201:    }
202:
203:    private synchronized void open(java.util.HashMap props) throws Exception
204:    {
205:        synchronized(s_databases)
206:        {
207:            s_databases.add(this );
208:        }
209:
210:        m_txTable = new java.util.HashMap();
211:        m_usedConnections = new java.util.HashSet();
212:        m_availableConnections = new java.util.TreeSet();
213:
214:        m_host = (String) props.get(PROP_HOST);
215:        m_port = ((Integer) props.get(PROP_PORT)).intValue();
216:        m_protocol = (String) props.get(PROP_PROTOCOL);
217:        m_username = (String) props.get(PROP_USERNAME);
218:        m_password = (String) props.get(PROP_PASSWORD);
219:        m_passPhrase = (String) props.get(PROP_PASSPHRASE);
220:
221:        m_connectTimeout = (Integer) props.get(PROP_CONNECT_TIMEOUT);
222:        m_context = props.get(PROP_CONTEXT);
223:
224:        if (m_context != null)
225:        {
226:            if ((m_protocol.indexOf("tcp") != -1) && ((m_context instanceof  SSLContext) == false))
227:            {
228:                throw new org.myoodb.exception.PermissionException("Invalid context for protocol: " + m_protocol);
229:            }
230:            else if ((m_protocol.indexOf("http") != -1) && ((m_context instanceof  String) == false))
231:            {
232:                throw new org.myoodb.exception.PermissionException("Invalid context for protocol: " + m_protocol);
233:            }
234:        }
235:
236:        if (m_passPhrase != null)
237:        {
238:            org.myoodb.core.Crypto crypto = new org.myoodb.core.Crypto(m_passPhrase);
239:            m_username = crypto.encrypt(m_username);
240:            m_password = crypto.encrypt(m_password);
241:        }
242:
243:        releaseConnection(acquireConnection(m_connectTimeout));
244:    }
245:
246:    private static java.util.HashMap createProperties(String uriString) throws java.net.URISyntaxException
247:    {
248:        java.util.HashMap props = new java.util.HashMap();
249:        java.net.URI url = new java.net.URI(uriString);
250:
251:        String host = url.getHost();
252:        if (host == null)
253:        {
254:            throw new java.net.URISyntaxException(uriString, "Missing Host");
255:        }
256:
257:        props.put(PROP_HOST, host);
258:
259:        int port = url.getPort();
260:        if (port == -1)
261:        {
262:            throw new java.net.URISyntaxException(uriString, "Missing Port");
263:        }
264:
265:        props.put(PROP_PORT, new Integer(port));
266:
267:        String protocol = url.getScheme();
268:        if (protocol == null)
269:        {
270:            throw new java.net.URISyntaxException(uriString, "Missing Protocol");
271:        }
272:
273:        props.put(PROP_PROTOCOL, protocol);
274:
275:        return props;
276:    }
277:
278:    private AbstractConnection internalLeaveTransaction(MyOodbTransaction tx)
279:    {
280:        AbstractConnection connection = null;
281:
282:        synchronized(m_txTable)
283:        {
284:            Thread thread = tx.getTransactionThread();
285:
286:            java.util.LinkedList txList = getThreadListByThread(thread);
287:            if (txList != null)
288:            {
289:                txList.remove(tx);
290:                if (txList.size() == 0)
291:                {
292:                    m_txTable.remove(thread);
293:                    connection = tx.getDatabaseConnection();
294:                }
295:            }
296:
297:            tx.setDatabaseConnection(null);
298:        }
299:
300:        return connection;
301:    }
302:
303:    private Object sendTransactionCommand(MyOodbTransaction tx, TransactionCommand command) throws Exception
304:    {
305:        try
306:        {
307:            return send(command, tx.getDatabaseConnection(), -1);
308:        }
309:        catch (RuntimeException e)
310:        {
311:            throw e;
312:        }
313:        catch (Exception e)
314:        {
315:            throw new org.myoodb.exception.TransactionException("Caught during send: " + e, e);
316:        }
317:    }
318:
319:    private AbstractConnection acquireConnection(int timeout) throws Exception
320:    {
321:        if (isOpen() == false)
322:        {
323:            throw new org.myoodb.exception.PermissionException("Database is not open");
324:        }
325:
326:        AbstractConnection connection = null;
327:
328:        do
329:        {
330:            if (m_availableConnections.size() == 0)
331:            {
332:                connection = newConnection(timeout);
333:            }
334:            else
335:            {
336:                synchronized(m_availableConnections)
337:                {
338:                    synchronized(m_usedConnections)
339:                    {
340:                        if (m_availableConnections.size() == 0)
341:                        {
342:                            continue;
343:                        }
344:
345:                        connection = (AbstractConnection) m_availableConnections.first();
346:                        m_availableConnections.remove(connection);
347:                    }
348:                }
349:
350:                if (connection.isConnected() == false)
351:                {
352:                    close(connection);
353:                    connection = null;
354:                }
355:            }
356:        }
357:        while (connection == null);
358:
359:        synchronized(m_availableConnections)
360:        {
361:            synchronized(m_usedConnections)
362:            {
363:                if (m_usedConnections.add(connection) == false)
364:                {
365:                    throw new org.myoodb.exception.PermissionException("Connection already in use");
366:                }
367:            }
368:        }
369:
370:        return connection;
371:    }
372:
373:    private void releaseConnection(AbstractConnection connection)
374:    {
375:        // XXX: removes a lot of duplicate code to check in here
376:        if (connection == null)
377:        {
378:            return;
379:        }
380:
381:        try
382:        {
383:            synchronized(m_availableConnections)
384:            {
385:                synchronized(m_usedConnections)
386:                {
387:                    m_usedConnections.remove(connection);
388:                    m_availableConnections.add(connection);
389:                }
390:            }
391:        }
392:        catch (java.lang.NullPointerException e)
393:        {
394:            // nothing to do
395:        }
396:    }
397:
398:    private void closeConnection(AbstractConnection connection)
399:    {
400:        // XXX: removes a lot of duplicate code to check in here
401:        if (connection == null)
402:        {
403:            return;
404:        }
405:
406:        try
407:        {
408:            synchronized(m_availableConnections)
409:            {
410:                synchronized(m_usedConnections)
411:                {
412:                    m_usedConnections.remove(connection);
413:                    m_availableConnections.remove(connection);
414:                }
415:            }
416:        }
417:        catch (java.lang.NullPointerException e)
418:        {
419:            // nothing to do
420:        }
421:
422:        try
423:        {
424:            connection.close();
425:        }
426:        catch (java.io.IOException e)
427:        {
428:            // nothing to do
429:        }
430:    }
431:
432:    private Object send(AbstractCommand command, int requestTimeout) throws Exception
433:    {
434:        if (isOpen() == false)
435:        {
436:            throw new org.myoodb.exception.PermissionException("Database is not open");
437:        }
438:
439:        Object result = null;
440:        AbstractConnection connection = null;
441:        MyOodbTransaction tx = (MyOodbTransaction) getTransactionByThread(Thread.currentThread());
442:
443:        try
444:        {
445:            if (tx != null)
446:            {
447:                if (tx.getDatabaseConnection() == null)
448:                {
449:                    throw new org.myoodb.exception.TransactionException("Transaction has no connection");
450:                }
451:
452:                connection = tx.getDatabaseConnection();
453:                result = send(command, connection, requestTimeout);
454:            }
455:            else
456:            {
457:                connection = acquireConnection(requestTimeout);
458:
459:                try
460:                {
461:                    result = send(command, connection, requestTimeout);
462:                    releaseConnection(connection);
463:                }
464:                catch (Exception e)
465:                {
466:                    if ((e instanceof  java.io.IOException) == false)
467:                    {
468:                        releaseConnection(connection);
469:                    }
470:
471:                    throw e;
472:                }
473:            }
474:        }
475:        catch (java.io.IOException e)
476:        {
477:            if (tx != null)
478:            {
479:                internalLeaveTransaction(tx);
480:            }
481:
482:            closeConnection(connection);
483:
484:            throw e;
485:        }
486:
487:        return result;
488:    }
489:
490:    private Object send(AbstractCommand command, AbstractConnection connection, int requestTimeout) throws Exception
491:    {
492:        Object result = null;
493:
494:        synchronized(connection)
495:        {
496:            if (connection instanceof  org.myoodb.core.DatagramConnection)
497:            {
498:                ((org.myoodb.core.DatagramConnection) connection).send(command, requestTimeout);
499:            }
500:            else
501:            {
502:                connection.send(command);
503:            }
504:
505:            result = connection.receive(requestTimeout);
506:            command.setResult(result);
507:
508:            if (result instanceof  Exception)
509:            {
510:                throw (Exception) result;
511:            }
512:        }
513:
514:        return result;
515:    }
516:
517:    private AbstractConnection newConnection(int timeout) throws Exception
518:    {
519:        Long id = null;
520:        AbstractConnection connection = null;
521:
522:        synchronized(s_databases)
523:        {
524:            id = new Long(m_connectIdentifier++);
525:        }
526:
527:        if (m_protocol.equals("tcp") == true)
528:        {
529:            connection = new org.myoodb.core.DatabaseConnection(this , id, m_host, m_port, timeout, false, (SSLContext) m_context);
530:        }
531:        else if (m_protocol.equals("tcps") == true)
532:        {
533:            connection = new org.myoodb.core.DatabaseConnection(this , id, m_host, m_port, timeout, true, (SSLContext) m_context);
534:        }
535:        else if (m_protocol.equals("http") == true)
536:        {
537:            connection = new org.myoodb.core.ServletConnection(this , id, m_host, m_port, timeout, false, s_tunnelPassThroughFlag, (String) m_context);
538:        }
539:        else if (m_protocol.equals("https") == true)
540:        {
541:            connection = new org.myoodb.core.ServletConnection(this , id, m_host, m_port, timeout, true, s_tunnelPassThroughFlag, (String) m_context);
542:        }
543:        else if (m_protocol.equals("udp") == true)
544:        {
545:            connection = new org.myoodb.core.DatagramConnection(this , id, m_host, m_port, timeout, false, s_tunnelPassThroughFlag);
546:        }
547:        else if (m_protocol.equals("udps") == true)
548:        {
549:            connection = new org.myoodb.core.DatagramConnection(this , id, m_host, m_port, timeout, true, s_tunnelPassThroughFlag);
550:        }
551:
552:        send(new org.myoodb.core.command.LoginCommand(m_username, m_password, (m_passPhrase != null)), connection, timeout);
553:
554:        return connection;
555:    }
556:
557:    protected void beginTransaction(MyOodbTransaction tx) throws Exception
558:    {
559:        synchronized(tx)
560:        {
561:            joinTransaction(tx);
562:
563:            TransactionCommand command = new TransactionCommand(TransactionCommand.REQUEST_BEGIN);
564:            sendTransactionCommand(tx, command);
565:
566:            tx.setTransactionIdentifier(((Long) command.getResult()).longValue());
567:        }
568:    }
569:
570:    protected void joinTransaction(MyOodbTransaction tx) throws Exception
571:    {
572:        synchronized(m_txTable)
573:        {
574:            Thread thread = Thread.currentThread();
575:            tx.setTransactionThread(thread);
576:
577:            java.util.LinkedList txList = getThreadListByThread(thread);
578:            if (txList == null)
579:            {
580:                txList = new java.util.LinkedList();
581:                m_txTable.put(thread, txList);
582:
583:                try
584:                {
585:                    if (tx.getDatabaseConnection() == null)
586:                    {
587:                        tx.setDatabaseConnection(acquireConnection(m_connectTimeout));
588:                    }
589:                }
590:                catch (Exception e)
591:                {
592:                    throw new org.myoodb.exception.TransactionException("Caught during join: " + e, e);
593:                }
594:            }
595:            else
596:            {
597:                tx.setDatabaseConnection(((MyOodbTransaction) txList.getFirst()).getDatabaseConnection());
598:            }
599:
600:            txList.add(tx);
601:        }
602:    }
603:
604:    protected void transferTransaction(MyOodbTransaction tx, Thread thread)
605:    {
606:        synchronized(m_txTable)
607:        {
608:            java.util.Iterator iter = m_txTable.values().iterator();
609:            while (iter.hasNext())
610:            {
611:                java.util.LinkedList txList = (java.util.LinkedList) iter.next();
612:
613:                if (txList.contains(tx) == true)
614:                {
615:                    iter.remove();
616:
617:                    iter = txList.iterator();
618:                    while (iter.hasNext())
619:                    {
620:                        tx = (MyOodbTransaction) iter.next();
621:                        tx.setTransactionThread(thread);
622:                    }
623:
624:                    m_txTable.put(thread, txList);
625:                    break;
626:                }
627:            }
628:        }
629:    }
630:
631:    protected boolean leaveTransaction(MyOodbTransaction tx) throws Exception
632:    {
633:        return (internalLeaveTransaction(tx) != null) ? true : false;
634:    }
635:
636:    protected void commitTransaction(MyOodbTransaction tx) throws Exception
637:    {
638:        synchronized(tx)
639:        {
640:            TransactionCommand command = new TransactionCommand(TransactionCommand.REQUEST_COMMIT);
641:            sendTransactionCommand(tx, command);
642:
643:            AbstractConnection connection = internalLeaveTransaction(tx);
644:            releaseConnection(connection);
645:        }
646:    }
647:
648:    protected void rollbackTransaction(MyOodbTransaction tx) throws Exception
649:    {
650:        synchronized(tx)
651:        {
652:            TransactionCommand command = new TransactionCommand(TransactionCommand.REQUEST_ROLLBACK);
653:            sendTransactionCommand(tx, command);
654:
655:            AbstractConnection connection = internalLeaveTransaction(tx);
656:            releaseConnection(connection);
657:        }
658:    }
659:
660:    protected int getStatusTransaction(MyOodbTransaction tx) throws Exception
661:    {
662:        synchronized(tx)
663:        {
664:            TransactionCommand command = new TransactionCommand(TransactionCommand.REQUEST_STATUS);
665:            sendTransactionCommand(tx, command);
666:
667:            return ((Integer) command.getResult()).intValue();
668:        }
669:    }
670:
671:    protected void finalize() throws Throwable
672:    {
673:        close();
674:    }
675:
676:    public final MyOodbTransaction createTransaction()
677:    {
678:        return new MyOodbTransaction(this );
679:    }
680:
681:    public final MyOodbTransaction currentTransaction()
682:    {
683:        return getTransactionByThread(Thread.currentThread());
684:    }
685:
686:    public final boolean isOpen()
687:    {
688:        return (m_txTable != null);
689:    }
690:
691:    public final void reopen() throws Exception
692:    {
693:        close();
694:
695:        m_txTable = new java.util.HashMap();
696:        m_usedConnections = new java.util.HashSet();
697:        m_availableConnections = new java.util.TreeSet();
698:
699:        synchronized(s_databases)
700:        {
701:            s_databases.add(this );
702:        }
703:
704:        releaseConnection(acquireConnection(m_connectTimeout));
705:    }
706:
707:    public final synchronized void close()
708:    {
709:        if (isOpen() == true)
710:        {
711:            synchronized(s_databases)
712:            {
713:                s_databases.remove(this );
714:            }
715:
716:            try
717:            {
718:                synchronized(m_availableConnections)
719:                {
720:                    synchronized(m_usedConnections)
721:                    {
722:                        m_availableConnections.addAll(m_usedConnections);
723:                        closeAvailableConnections(0);
724:                    }
725:                }
726:            }
727:            finally
728:            {
729:                m_txTable = null;
730:                m_usedConnections = null;
731:                m_availableConnections = null;
732:            }
733:        }
734:    }
735:
736:    public final int numberOfUsedConnections()
737:    {
738:        return m_usedConnections.size();
739:    }
740:
741:    public final int numberOfAvailableConnections()
742:    {
743:        return m_availableConnections.size();
744:    }
745:
746:    public final synchronized void closeAvailableConnections(int lowWaterMark)
747:    {
748:        synchronized(m_availableConnections)
749:        {
750:            if (isOpen() == false)
751:            {
752:                throw new org.myoodb.exception.PermissionException("Database is not open");
753:            }
754:
755:            synchronized(m_usedConnections)
756:            {
757:                java.util.Iterator iter = m_availableConnections.iterator();
758:                while (iter.hasNext())
759:                {
760:                    if (m_availableConnections.size() <= lowWaterMark)
761:                    {
762:                        break;
763:                    }
764:
765:                    close((AbstractConnection) iter.next());
766:                    iter.remove();
767:                }
768:            }
769:        }
770:    }
771:
772:    public final int getPort()
773:    {
774:        return m_port;
775:    }
776:
777:    public final String getHost()
778:    {
779:        return m_host;
780:    }
781:
782:    public final String getProtocol()
783:    {
784:        return m_protocol;
785:    }
786:
787:    public final void setCommunicationInterfacePassword(String oldPassword, String newPassword)
788:    {
789:        if (m_password.compareTo(oldPassword) != 0)
790:        {
791:            throw new org.myoodb.exception.PermissionException("Invalid Password");
792:        }
793:
794:        m_password = newPassword;
795:    }
796:
797:    public final MyOodbProxy createRoot(String rootName, String className, String sig, Object[] args)
798:    {
799:        try
800:        {
801:            return (MyOodbProxy) send(new org.myoodb.core.command.CreateCommand(rootName, className, sig, args), -1);
802:        }
803:        catch (RuntimeException e)
804:        {
805:            throw e;
806:        }
807:        catch (Exception e)
808:        {
809:            throw new org.myoodb.exception.InternalException("Caught during create root: " + e, e);
810:        }
811:    }
812:
813:    public final MyOodbProxy createObject(String className, String sig, Object[] args)
814:    {
815:        try
816:        {
817:            return (MyOodbProxy) send(new org.myoodb.core.command.CreateCommand(className, sig, args), -1);
818:        }
819:        catch (RuntimeException e)
820:        {
821:            throw e;
822:        }
823:        catch (Exception e)
824:        {
825:            throw new org.myoodb.exception.InternalException("Caught during create object: " + e, e);
826:        }
827:    }
828:
829:    public final void deleteObject(MyOodbRemote obj)
830:    {
831:        try
832:        {
833:            send(new org.myoodb.core.command.DeleteCommand(obj.getDatabaseHandle()), -1);
834:        }
835:        catch (RuntimeException e)
836:        {
837:            throw e;
838:        }
839:        catch (Exception e)
840:        {
841:            throw new org.myoodb.exception.InternalException("Caught during delete: " + e, e);
842:        }
843:    }
844:
845:    public final MyOodbProxy getRoot(String rootName)
846:    {
847:        try
848:        {
849:            return (MyOodbProxy) send(new org.myoodb.core.command.GetRootCommand(rootName), -1);
850:        }
851:        catch (RuntimeException e)
852:        {
853:            throw e;
854:        }
855:        catch (Exception e)
856:        {
857:            throw new org.myoodb.exception.InternalException("Caught during get root: " + e, e);
858:        }
859:    }
860:
861:    public final MyOodbLocal getObject(MyOodbRemote obj)
862:    {
863:        try
864:        {
865:            org.myoodb.core.MyOodbManager manager = org.myoodb.core.MyOodbManager.getTheManager();
866:            return manager.getStoreManager().getContainer(null, obj.getDatabaseHandle()).getTarget();
867:        }
868:        catch (RuntimeException e)
869:        {
870:            throw e;
871:        }
872:        catch (Exception e)
873:        {
874:            throw new org.myoodb.exception.InternalException("Caught during get object: " + e, e);
875:        }
876:    }
877:
878:    public final MyOodbProxy getObject(org.myoodb.core.Identifier handle)
879:    {
880:        try
881:        {
882:            return (MyOodbProxy) send(new org.myoodb.core.command.GetObjectCommand(handle), -1);
883:        }
884:        catch (RuntimeException e)
885:        {
886:            throw e;
887:        }
888:        catch (Exception e)
889:        {
890:            throw new org.myoodb.exception.InternalException("Caught during get object: " + e, e);
891:        }
892:    }
893:
894:    public final void setBean(org.myoodb.core.Identifier handle, MyOodbBean bean)
895:    {
896:        try
897:        {
898:            send(new org.myoodb.core.command.SetBeanCommand(handle, bean), -1);
899:        }
900:        catch (RuntimeException e)
901:        {
902:            throw e;
903:        }
904:        catch (Exception e)
905:        {
906:            throw new org.myoodb.exception.InternalException("Caught during set bean: " + e, e);
907:        }
908:    }
909:
910:    public final MyOodbBean getBean(org.myoodb.core.Identifier handle)
911:    {
912:        try
913:        {
914:            return (MyOodbBean) send(new org.myoodb.core.command.GetBeanCommand(handle), -1);
915:        }
916:        catch (RuntimeException e)
917:        {
918:            throw e;
919:        }
920:        catch (Exception e)
921:        {
922:            throw new org.myoodb.exception.InternalException("Caught during get bean: " + e, e);
923:        }
924:    }
925:
926:    public final void setXML(org.myoodb.core.Identifier handle, String xml)
927:    {
928:        try
929:        {
930:            send(new org.myoodb.core.command.SetXMLCommand(handle, xml), -1);
931:        }
932:        catch (RuntimeException e)
933:        {
934:            throw e;
935:        }
936:        catch (Exception e)
937:        {
938:            throw new org.myoodb.exception.InternalException("Caught during set xml: " + e, e);
939:        }
940:    }
941:    
942:    public final String getXML(org.myoodb.core.Identifier handle)
943:    {
944:        try 
945:        {
946:            return (String) send(new org.myoodb.core.command.GetXMLCommand(handle), -1);
947:        }
948:        catch (RuntimeException e)
949:        {
950:            throw e;
951:        }
952:        catch (Exception e)
953:        {
954:            throw new org.myoodb.exception.InternalException("Caught during get xml: " + e, e);
955:        }
956:    }
957:
958:    public final Object invokeMethod(MyOodbRemote obj, String methodName, String sig, Object[] args, int accessLevel, int requestTimeout)
959:    {
960:        try
961:        {
962:            return send(new org.myoodb.core.command.InvokeMethodCommand(obj.getDatabaseHandle(), methodName, sig, args, accessLevel), requestTimeout);
963:        }
964:        catch (RuntimeException e)
965:        {
966:            throw e;
967:        }
968:        catch (Exception e)
969:        {
970:            throw new org.myoodb.exception.InternalException("Caught during invoke method: " + e, e);
971:        }
972:    }
973:
974:    public final Object invokeMethod(MyOodbRemote obj, int methodIndex, Object[] args, int accessLevel, int requestTimeout)
975:    {
976:        try
977:        {
978:            return send(new org.myoodb.core.command.InvokeMethodCommand(obj.getDatabaseHandle(), methodIndex, args, accessLevel), requestTimeout);
979:        }
980:        catch (RuntimeException e)
981:        {
982:            throw e;
983:        }
984:        catch (Exception e)
985:        {
986:            throw new org.myoodb.exception.InternalException("Caught during invoke method: " + e, e);
987:        }
988:    }
989:}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.