Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.orsup.nonpublic;

import org.litesoft.changemanagement.*;
import org.litesoft.commonfoundation.typeutils.Objects;
import org.litesoft.db.*;
import org.litesoft.orsup.base.*;
import org.litesoft.orsup.transact.*;
import org.litesoft.orsup.transact.nonpublic.*;

import java.io.*;
import java.util.*;

public class TransactionImpl extends AbstractFinder implements Transaction,
                                                               TransactionBackdoor
{
    private final TransParticipantsManager mParticipantsManager = new TransParticipantsManager();

    private final NonTransactionalFinder mNonTransactionalFinder;
    private final long mID;
    private final Map<String, Serializable> mXtraData = new HashMap<String, Serializable>();
    private final Map<Object, Object> mProperties = new HashMap<Object, Object>();

    private final HistoryAugmentor mHistoryAugmentor;
    private final TAnodeManager<DA_LinkListNode> mDeleteAugmentors = new TAnodeManager<DA_LinkListNode>();
    private final TAnodeManager<CA_LinkListNode> mCommitAugmentors = new TAnodeManager<CA_LinkListNode>();

    private String mTransactionSourceClassname = null;

    public TransactionImpl( NonTransactionalFinder pNonTransactionalFinder, boolean pAugment )
    {
        super( ORFacilitatorImpl.getInstance( pNonTransactionalFinder ), LOGGER, pNonTransactionalFinder.isIncludingImmortalInactives(), //
               pNonTransactionalFinder.getPOQueryFilterFactory() );
        mNonTransactionalFinder = pNonTransactionalFinder;
        mID = getNextSequenceNumber( "Transactions" );

        HistoryAugmentorFactory zHAfactory = null;
        TransactionAugmentorFactory[] zTAfactories = null;
        if ( pAugment )
        {
            TransactionAugmentationManager zTAM = getTransactionAugmentationManager();
            //noinspection SynchronizationOnLocalVariableOrMethodParameter
            synchronized ( zTAM )
            {
                zHAfactory = zTAM.getHistoryAugmentorFactory();
                zTAfactories = // Grab the current list
                        (zTAM instanceof TransactionAugmentationManagerInternalExtension) ? ((TransactionAugmentationManagerInternalExtension) zTAM).getRawTransactionAugmentorsArray() : zTAM.getTransactionAugmentors();
            }
        }
        mHistoryAugmentor = (zHAfactory != null) ? zHAfactory.createHistoryAugmentor() : null;
        if ( zTAfactories != null )
        {
            for ( TransactionAugmentorFactory zTAFactory : zTAfactories )
            {
                addTransactionAugmentor( zTAFactory );
            }
        }
    }

    @Override
    public final NonTransactionalFinder getNonTransactionalFinder()
    {
        return mNonTransactionalFinder;
    }

    @Override
    public final long getTransactionID()
    {
        return mID;
    }

    @Override
    public final Serializable getXtraData( Class pForKey )
    {
        return (pForKey == null) ? null : mXtraData.get( pForKey.getName() );
    }

    @Override
    public final Transaction setXtraData( Class pForKey, Serializable pData )
    {
        if ( pForKey != null )
        {
            if ( pData == null )
            {
                mXtraData.remove( pForKey.getName() );
            }
            else
            {
                mXtraData.put( pForKey.getName(), pData );
            }
        }
        return this;
    }

    @Override
    public final void addProperty( Object pKey, Object pValue )
    {
        Objects.assertNotNull( "Key", pKey );
        if ( pValue != null )
        {
            mProperties.put( pKey, pValue );
        }
        else
        {
            mProperties.remove( pKey );
        }
    }

    @Override
    public final Object getProperty( Object pKey )
    {
        Objects.assertNotNull( "Key", pKey );
        return mProperties.get( pKey );
    }

    @Override
    public String getTransactionSourceClassname()
    {
        return mTransactionSourceClassname;
    }

    @Override
    public Transaction setTransactionSource( Class pSourceClass )
    {
        mTransactionSourceClassname = (pSourceClass != null) ? pSourceClass.getName() : null;
        return this;
    }

    @Override
    public Transaction addTransactionAugmentor( TransactionAugmentorFactory pTransactionAugmentorFactory )
            throws IllegalStateException
    {
        return (pTransactionAugmentorFactory == null) ? //
               this : //
               addTransactionAugmentor( pTransactionAugmentorFactory.createTransactionAugmentor( this ) );
    }

    @Override
    public Transaction addTransactionAugmentor( TransactionAugmentor pTransactionAugmentor )
            throws IllegalStateException
    {
        if ( pTransactionAugmentor instanceof CommitAugmentor )
        {
            addCommitAugmentor( (CommitAugmentor) pTransactionAugmentor );
        }
        if ( pTransactionAugmentor instanceof DeleteAugmentor )
        {
            addDeleteAugmentor( (DeleteAugmentor) pTransactionAugmentor );
        }
        return this;
    }

    @Override
    public Transaction addCommitAugmentor( CommitAugmentor pCommitAugmentor )
    {
        if ( null != pCommitAugmentor )
        {
            mCommitAugmentors.add( new CA_LinkListNode( pCommitAugmentor ) );
        }
        return this;
    }

    @Override
    public CommitAugmentor[] getCommitAugmentors()
    {
        CA_LinkListNode zHead = mCommitAugmentors.getHead();
        if ( zHead == null )
        {
            return CommitAugmentor.EMPTY_ARRAY;
        }
        CommitAugmentor[] rv = new CommitAugmentor[zHead.getLength()];
        zHead.populate( rv );
        return rv;
    }

    @Override
    public boolean hasCommitAugmentor( Class<? extends CommitAugmentor> pAugmentorClass )
    {
        return mCommitAugmentors.hasAugmentor( pAugmentorClass );
    }

    @Override
    public boolean removeCommitAugmentor( Class<? extends CommitAugmentor> pAugmentorClass )
    {
        return mCommitAugmentors.removeAugmentor( pAugmentorClass );
    }

    @Override
    public Transaction addDeleteAugmentor( DeleteAugmentor pDeleteAugmentor )
            throws IllegalStateException
    {
        if ( null != pDeleteAugmentor )
        {
            if ( !mParticipantsManager.isEmpty() )
            {
                throw new IllegalStateException( "DeleteAugmentor may only be added BEFORE any POs are added to the Transaction" );
            }
            mDeleteAugmentors.add( new DA_LinkListNode( pDeleteAugmentor ) );
        }
        return this;
    }

    @Override
    public DeleteAugmentor[] getDeleteAugmentors()
    {
        DA_LinkListNode zHead = mDeleteAugmentors.getHead();
        if ( zHead == null )
        {
            return DeleteAugmentor.EMPTY_ARRAY;
        }
        DeleteAugmentor[] rv = new DeleteAugmentor[zHead.getLength()];
        zHead.populate( rv );
        return rv;
    }

    @Override
    public boolean hasDeleteAugmentor( Class<? extends DeleteAugmentor> pAugmentorClass )
    {
        return mDeleteAugmentors.hasAugmentor( pAugmentorClass );
    }

    @Override
    public boolean removeDeleteAugmentor( Class<? extends DeleteAugmentor> pAugmentorClass )
    {
        return mDeleteAugmentors.removeAugmentor( pAugmentorClass );
    }

    @Override
    public PersistentObject<?> create( String pPersistedObjectRegistrationName )
            throws DBException
    {
        return getRequiredMetaDataForPO( pPersistedObjectRegistrationName ).createNew( this );
    }

    @SuppressWarnings({"unchecked"}) @Override
    public <T extends PersistentObject> T create( Class<T> pPersistedObjectClass )
            throws DBException
    {
        return (T) create( pPersistedObjectClass.getName() );
    }

    @Override
    public PersistentObject<?> getPersistentObjectFromIdentityMap( PersistentObjectURL pPersistentObjectURL )
    {
        return (pPersistentObjectURL != null) ? mParticipantsManager.get( pPersistentObjectURL ) : null;
    }

    /**
     * Refresh the pPersistedObject into this transaction if it is not already in the Transaction.
     *
     * @param pPersistedObject - Persisted Object to be freshly read (if needed)
     *
     * @return Transactional copy of the latest version read via Transaction (if needed), or Null if the PO is in another Transaction and isNew() OR has been deleted
     *
     * @throws DBException - Exception regarding the OR layer
     */
    @SuppressWarnings({"unchecked"})
    public <T extends PersistentObject> T freshIntoIfNotInTransaction( PersistentObject<T> pPersistedObject )
            throws DBException
    {
        if ( pPersistedObject == null )
        {
            return null;
        }
        PersistentObjectURL url = pPersistedObject.getPersistentObjectURL();
        PersistentObject<?> existing = getPersistentObjectFromIdentityMap( url );
        if ( existing != null )
        {
            return (T) existing;
        }
        // todo: Caches
        return (T) findOne( url );
    }

    /**
     * Refresh the pPersistedObject into this transaction.
     *
     * @param pPersistedObject - Persisted Object to be freshly read
     *
     * @return Transactional copy of the latest version read via Transaction, or Null if the PO is in another Transaction and isNew() OR has been deleted
     *
     * @throws UnsupportedOperationException - if the PO is already in the Transaction
     * @throws DBException                   - Exception regarding the OR layer
     */
    @Override
    public <T extends PersistentObject> T refreshInto( PersistentObject<T> pPersistedObject )
            throws UnsupportedOperationException, DBException
    {
        if ( pPersistedObject == null )
        {
            return null;
        }
        PersistentObjectURL url = pPersistedObject.getPersistentObjectURL();
        PersistentObject<?> existing = getPersistentObjectFromIdentityMap( url );
        if ( existing != null )
        {
            throw new UnsupportedOperationException( pPersistedObject.getDisplayValue() + " already in the Transaction" );
        }
        // todo: Caches
        //noinspection unchecked
        return (T) findOne( url );
    }

    @Override
    public final String getState()
    {
        return mParticipantsManager.getState().toString();
    }

    /**
     * This method returns true if the PO & transaction allows modification.
     * <p/>
     * This Transaction will only return true if it is not in the LL_COMMITING state AND has not been: successfully committed or disposed.
     */
    @Override
    public boolean supportsPOmodification( PersistentObject pPoToModify )
    {
        return mParticipantsManager.getState().supportsPOmodification();
    }

    @Override
    public final boolean isFunctional()
    {
        return mParticipantsManager.getState().isFunctional();
    }

    @Override
    public void dispose()
    {
        mParticipantsManager.dispose();
    }

    @Override
    protected void finalize()
            throws Throwable
    {
        dispose();
        super.finalize();
    }

    @Override
    public PersistentObject[] getAllParticipants()
    {
        return mParticipantsManager.getAllParticipants();
    }

    /**
     * Commit changes from a temporary transaction space to the database
     * <p/>
     * This method must be called during an active transaction
     */
    @Override
    public void commit()
            throws ConcurrentPOModificationException, DBException
    {
        LLcommit( mCommitAugmentors.getHead() );
    }

    /**
     * Commit changes from a temporary transaction space to the database
     * <p/>
     * This method must be called during an active transaction
     */
    @Override
    public void commit( CommitAugmentor pCommitAugmentor )
            throws ConcurrentPOModificationException, DBException
    {
        CA_LinkListNode zNode = mCommitAugmentors.getHead();
        if ( pCommitAugmentor != null )
        {
            zNode = new CA_LinkListNode( pCommitAugmentor, zNode );
        }
        LLcommit( zNode );
    }

    /**
     * Under rare circumstances, a POs Key Vvlaues are changed and the Transaction must re-index it!
     * <p/>
     *
     * @param pOrigURL !null
     * @param pNewURL  !null
     */
    @Override
    public void changedURL( PersistentObjectURL pOrigURL, PersistentObjectURL pNewURL )
    {
        mParticipantsManager.changeURL( pOrigURL, pNewURL );
    }

    public void LLcommit( CA_LinkListNode pCommitAugmentors )
            throws ConcurrentPOModificationException, DBException
    {
        List<PersistentObjectImpl<?>> zParticipants = new ArrayList<PersistentObjectImpl<?>>();

        mParticipantsManager.initiateCommitting( zParticipants );

        DataStoreChangeCollector zAllCollector = new DataStoreChangeCollector( getTransactionID(), getTransactionSourceClassname(), mXtraData );
        try
        {
            if ( !zParticipants.isEmpty() )
            {
                TransCommitCollector collector = new TransCommitCollector( pCommitAugmentors );

                collector.add( zParticipants );

                List<PersistentObjectImpl<?>> zAdded;
                while ( !(zAdded = mParticipantsManager.grabAddedParticipantsDuringCommits()).isEmpty() )
                {
                    collector.add( zAdded );
                }
                if ( mHistoryAugmentor != null )
                {
                    collector.augment( mHistoryAugmentor );
                    if ( !(zAdded = mParticipantsManager.grabAddedParticipantsDuringCommits()).isEmpty() )
                    {
                        collector.justAdd( zAdded );
                    }
                    collector.validateNoChanges( "HistoryAugmentor" );
                }
                Set<PersistentObjectImpl> commiting = collector.getEntriesToCommit( zAllCollector );

                if ( !commiting.isEmpty() )
                {
                    mParticipantsManager.transitionToLowLevelCommitting();

                    TransactionCommitListenerManager zManager = getTransactionCommitListenerManager();
                    TransactionCommitListener[] zListeners = // Grab the current list
                            (zManager instanceof TransactionCommitListenerManagerInternalExtension) ? ((TransactionCommitListenerManagerInternalExtension) zManager).getRawTransactionCommitListenersArray() : zManager.getListeners();

                    if ( zListeners.length == 0 )
                    {
                        commitParticipants( commiting );
                    }
                    else
                    {
                        TransactionSuccessfulCommitCallBack[] zCallBacks = new TransactionSuccessfulCommitCallBack[zListeners.length];

                        PersistentObject[] zCommiting = commiting.toArray( new PersistentObject[commiting.size()] );

                        for ( int i = 0; i < zListeners.length; i++ )
                        {
                            try
                            {
                                zCallBacks[i] = zListeners[i].aboutToCommit( zCommiting );
                            }
                            catch ( RuntimeException e )
                            {
                                LOGGER.error.log( e );
                            }
                        }
                        commitParticipants( commiting );

                        for ( TransactionSuccessfulCommitCallBack zCallBack : zCallBacks )
                        {
                            if ( zCallBack != null )
                            {
                                try
                                {
                                    zCallBack.commitSucceeded();
                                }
                                catch ( RuntimeException e )
                                {
                                    LOGGER.error.log( e );
                                }
                            }
                        }
                    }
                }
                SucceededTransactionCommitListenerManager zManager = getSucceededTransactionCommitListenerManager();
                SucceededTransactionCommitListener[] zListeners = // Grab the current list
                        (zManager instanceof SucceededTransactionCommitListenerManagerInternalExtension) ? ((SucceededTransactionCommitListenerManagerInternalExtension) zManager).getRawTransactionCommitListenersArray() : zManager.getListeners();

                collector.commitSucceeded( mTransactionSourceClassname, zListeners, mParticipantsManager );
            }
            mParticipantsManager.successfullyCommitted();
        }
        finally
        {
            mParticipantsManager.clearCommitting();
        }

        if ( zAllCollector.isNotEmpty() )
        {
            ChangeListenerManager.get().notifyAllListeners( zAllCollector.createSet() );
        }
        dispose();
    }

    enum CommitAction
    {
        UPDATE, INSERT, DELETE, DELETE_NEW;

        public static CommitAction get( PersistentObjectImpl pPO )
        {
            return pPO.isNew() ? //
                   (pPO.isDeleteRequested() ? DELETE_NEW : INSERT) : //
                   (pPO.isDeleteRequested() ? DELETE : UPDATE);
        }
    }

    protected void commitParticipants( Set<PersistentObjectImpl> pParticipants )
            throws ConcurrentPOModificationException, DBException
    {
        PersistentObjectImpl[] zPOs = pParticipants.toArray( new PersistentObjectImpl[pParticipants.size()] );
        Arrays.sort( zPOs );
        DBconnection zDBconnection = null;
        try
        {
            zDBconnection = getPersistenceHelper().createConnection();
            zDBconnection.setAutoCommit( false );
            for ( PersistentObjectImpl po : zPOs )
            {
                processCUD( zDBconnection, po );
            }
            zDBconnection.commit();
        }
        finally
        {
            if ( null != zDBconnection )
            {
                zDBconnection.dispose();
            }
        }
    }

    private void processCUD( DBconnection pDBconnection, PersistentObjectImpl pPO )
    {
        CommitAction zCommitAction = CommitAction.get( pPO );
        try
        {
            TransactionPersistenceHelper helper = (TransactionPersistenceHelper) getPersistenceHelper();
            switch ( zCommitAction )
            {
                case INSERT:
                    helper.insert( pDBconnection, pPO, this );
                    return;
                case DELETE:
                    helper.delete( pDBconnection, pPO, this );
                    // fall thru
                case DELETE_NEW:
                    PersistentObjectImpl.BackDoor.justDeletedInTransaction( pPO, pDBconnection );
                    return;
                case UPDATE:
                    helper.update( pDBconnection, pPO, this );
                    return;
                default:
                    break; // Will Throw IllegalStateException
            }
        }
        catch ( DBException e )
        {
            e.setAugmentedMessage( " on " + pPO.getObjectName() + ": " + pPO );
            throw e;
        }
        catch ( ConcurrentPOModificationException e )
        {
            e.setWithPO( pPO );
            throw e;
        }
        throw new IllegalStateException( "Unexpected CommitAction '" + zCommitAction + "' on " + pPO.getObjectName() + ": " + pPO );
    }

    @Override
    protected FindOneCacheResult checkCacheFindOne( String pPersistedObjectRegistrationName, //
                                                    PersistentObjectUniqueKey pPersistentObjectUniqueKey )
            throws DBException
    {
        // 1st we need to check into our transaction...
        PersistentObject<?> po = getPersistentObjectFromIdentityMap( new PersistentObjectURL( pPersistedObjectRegistrationName, pPersistentObjectUniqueKey ) );
        return (po != null) ? new FindOneCacheResult( po ) : null;
    }

    @Override
    public String toString()
    {
        return "Transaction: " + getTransactionID();
    }

    // TransactionBackdoor

    /**
     * When a PO has had its requestDelete() called on it, this method will be called BEFORE
     * any request delete processing occures.
     *
     * @param pPersistentObject that has just had its requestDelete() called on it.
     *
     * @return false if the PO should NOT be deleted!
     */
    @Override
    public boolean getApprovalFromDeleteAugmentors( PersistentObject<?> pPersistentObject )
    {
        boolean rv = true;
        for ( DA_LinkListNode zNode = mDeleteAugmentors.getHead(); zNode != null; zNode = zNode.getNext() )
        {
            if ( !zNode.getDeleteAugmentor().approveDeleteRequest( pPersistentObject ) )
            {
                rv = false;
            }
        }
        return rv;
    }

    @SuppressWarnings("unchecked") @Override
    public <T extends PersistentObjectImpl> T addPO( T pPersistentObject )
    {
        return (T) mParticipantsManager.addPO( pPersistentObject );
    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/orsup/nonpublic/TransactionImpl.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
239 Diff Diff GeorgeS picture GeorgeS Wed 01 Jun, 2011 06:34:52 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

24 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 01:51:38 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000