#########
# Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  * See the License for the specific language governing permissions and
#  * limitations under the License.

__author__ = 'idanmo'

import imp
import sys
from os import path

storage_manager_module_name = 'file_storage_manager'
#storage_manager_module_name = 'es_storage_manager'

_instance = None


def _create_instance():
    paths = sys.path
    paths.append(path.dirname(__file__))
    return imp.load_module(storage_manager_module_name,
                           *imp.find_module(
                               storage_manager_module_name, paths)).create()


def reset():
    global _instance
    _instance = _create_instance()


def instance():
    global _instance
    if _instance is None:
        _instance = _create_instance()
    return _instance

@@ -697,16 +697,105 @@
 t path%0A%0A
+from flask import g, current_app%0A%0Afrom manager_rest.util import maybe_register_teardown%0A%0A
 storage_
@@ -1186,24 +1186,56 @@
 ef reset():%0A
+    # print %22resetting storage%22%0A
     global _
@@ -1382,16 +1382,16 @@
 tance()%0A
-
     retu
@@ -1395,16 +1395,426 @@
 eturn _instance%0A
+%0A%0A%0Adef teardown_storage_manager(exception):%0A    # print %22tearing down storage manager!%22%0A    pass%0A%0A%0Adef get_storage_manager():%0A    if 'storage_manager' not in g:%0A        g.storage_manager = _create_instance()  # TODO: make sure the import happens only once!%0A        maybe_register_teardown(current_app, teardown_storage_manager)%0A%0A    return g.storage_manager%0A%0A%0Adef get_storage_manager():%0A    return instance()%0A%0A

fix teardown handler registration