@Override
    public final void contextDestroyed(final ServletContextEvent sce) {

        Collection<AbstractMockEc2Instance> instances = MockEc2Controller.getInstance()
                    .getAllMockEc2Instances();

        for (AbstractMockEc2Instance instance : instances) {
                // cancel and destroy the internal timers for all instances on
                // web app stopping
            instance.destroyInternalTimer();
        }

        if (persistenceEnabled) {

            // put all instances into an array which is serializable and type-cast safe for persistence
            AbstractMockEc2Instance[] array = new AbstractMockEc2Instance[instances.size()];
            instances.toArray(array);
            PersistenceUtils.saveAll(array, PersistenceStoreType.EC2);


            Collection<MockVpc> vpcs = MockVpcController.getInstance()
                    .describeVpcs();
            MockVpc[] vpcArray = new MockVpc[vpcs.size()];
            vpcs.toArray(vpcArray);
            PersistenceUtils.saveAll(vpcArray, PersistenceStoreType.VPC);

            Collection<MockVolume> volumes = MockVolumeController.getInstance()
                    .describeVolumes();
            MockVolume[] volumeArray = new MockVolume[volumes.size()];
            volumes.toArray(volumeArray);
            PersistenceUtils.saveAll(volumeArray, PersistenceStoreType.VOLUME);

            Collection<MockTags> tags = MockTagsController.getInstance().describeTags();

            MockTags[] tagArray = new MockTags[tags.size()];
            tags.toArray(tagArray);
            PersistenceUtils.saveAll(tagArray, PersistenceStoreType.TAGS);

            Collection<MockSubnet> subnets = MockSubnetController.getInstance().describeSubnets();

            MockSubnet[] subnetArray = new MockSubnet[subnets.size()];
            subnets.toArray(subnetArray);
            PersistenceUtils.saveAll(subnetArray, PersistenceStoreType.SUBNET);

            Collection<MockRouteTable> routetables = MockRouteTableController.getInstance().describeRouteTables();

            MockRouteTable[] routetableArray = new MockRouteTable[routetables.size()];
            routetables.toArray(routetableArray);
            PersistenceUtils.saveAll(routetableArray, PersistenceStoreType.ROUTETABLE);

            Collection<MockInternetGateway> internetgateways = MockInternetGatewayController.getInstance()
                    .describeInternetGateways();

            MockInternetGateway[] internetgatewayArray = new MockInternetGateway[internetgateways.size()];
            internetgateways.toArray(internetgatewayArray);
            PersistenceUtils.saveAll(internetgatewayArray, PersistenceStoreType.INTERNETGATEWAY);
        }

        MockEc2Controller.getInstance().destroyCleanupTerminatedInstanceTimer();

        log.info("aws-mock stopped.");
    }
@ Override public final void contextDestroyed ( final ServletContextEvent sce ) { Collection < AbstractMockEc2Instance > instances = MockEc2Controller . getInstance ( ) . getAllMockEc2Instances ( ) ; for ( AbstractMockEc2Instance instance : instances ) { instance . destroyInternalTimer ( ) ; } if ( persistenceEnabled ) { AbstractMockEc2Instance [ ] array = new AbstractMockEc2Instance [ instances . size ( ) ] ; instances . toArray ( array ) ; PersistenceUtils . saveAll ( array , PersistenceStoreType . EC2 ) ; Collection < MockVpc > vpcs = MockVpcController . getInstance ( ) . describeVpcs ( ) ; MockVpc [ ] vpcArray = new MockVpc [ vpcs . size ( ) ] ; vpcs . toArray ( vpcArray ) ; PersistenceUtils . saveAll ( vpcArray , PersistenceStoreType . VPC ) ; Collection < MockVolume > volumes = MockVolumeController . getInstance ( ) . describeVolumes ( ) ; MockVolume [ ] volumeArray = new MockVolume [ volumes . size ( ) ] ; volumes . toArray ( volumeArray ) ; PersistenceUtils . saveAll ( volumeArray , PersistenceStoreType . VOLUME ) ; Collection < MockTags > tags = MockTagsController . getInstance ( ) . describeTags ( ) ; MockTags [ ] tagArray = new MockTags [ tags . size ( ) ] ; tags . toArray ( tagArray ) ; PersistenceUtils . saveAll ( tagArray , PersistenceStoreType . TAGS ) ; Collection < MockSubnet > subnets = MockSubnetController . getInstance ( ) . describeSubnets ( ) ; MockSubnet [ ] subnetArray = new MockSubnet [ subnets . size ( ) ] ; subnets . toArray ( subnetArray ) ; PersistenceUtils . saveAll ( subnetArray , PersistenceStoreType . SUBNET ) ; Collection < MockRouteTable > routetables = MockRouteTableController . getInstance ( ) . describeRouteTables ( ) ; MockRouteTable [ ] routetableArray = new MockRouteTable [ routetables . size ( ) ] ; routetables . toArray ( routetableArray ) ; PersistenceUtils . saveAll ( routetableArray , PersistenceStoreType . ROUTETABLE ) ; Collection < MockInternetGateway > internetgateways = MockInternetGatewayController . getInstance ( ) . describeInternetGateways ( ) ; MockInternetGateway [ ] internetgatewayArray = new MockInternetGateway [ internetgateways . size ( ) ] ; internetgateways . toArray ( internetgatewayArray ) ; PersistenceUtils . saveAll ( internetgatewayArray , PersistenceStoreType . INTERNETGATEWAY ) ; } MockEc2Controller . getInstance ( ) . destroyCleanupTerminatedInstanceTimer ( ) ; log . info ( "aws-mock stopped." ) ; }
We save the instances if persistence of enabled, on web application shutting-down.

@param sce
the context event object