-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
322 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 14 additions & 31 deletions
45
cat-client/src/main/java/com/dianping/cat/component/DefaultComponentLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,43 @@ | ||
package com.dianping.cat.component; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import com.dianping.cat.component.lifecycle.Disposable; | ||
import com.dianping.cat.component.lifecycle.Initializable; | ||
import com.dianping.cat.component.lifecycle.LogEnabled; | ||
import com.dianping.cat.component.lifecycle.Logger; | ||
|
||
public class DefaultComponentLifecycle implements ComponentLifecycle, Initializable, Disposable { | ||
public class DefaultComponentLifecycle implements ComponentLifecycle { | ||
private ComponentContext m_ctx; | ||
|
||
private Logger m_logger; | ||
|
||
private AtomicBoolean m_initialized = new AtomicBoolean(); | ||
|
||
public DefaultComponentLifecycle(ComponentContext ctx) { | ||
m_ctx = ctx; | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
if (m_initialized.get()) { | ||
m_logger = null; | ||
m_initialized.set(false); | ||
private Logger getLogger() { | ||
// lazy load to avoid cyclically dependency resolution | ||
if (m_logger == null) { | ||
m_logger = m_ctx.lookup(Logger.class); | ||
} | ||
} | ||
|
||
@Override | ||
public void initialize(ComponentContext ctx) { | ||
m_initialized.set(true); | ||
m_logger = m_ctx.lookup(Logger.class); | ||
return m_logger; | ||
} | ||
|
||
@Override | ||
public void onStart(Object component) { | ||
if (m_initialized.get()) { | ||
if (component instanceof LogEnabled) { | ||
((LogEnabled) component).enableLogging(m_logger); | ||
} | ||
|
||
if (component instanceof Initializable) { | ||
((Initializable) component).initialize(m_ctx); | ||
} | ||
} else { | ||
throw new IllegalStateException("Component lifecycle has been shutdown!"); | ||
if (component instanceof LogEnabled) { | ||
((LogEnabled) component).enableLogging(getLogger()); | ||
} | ||
|
||
if (component instanceof Initializable) { | ||
((Initializable) component).initialize(m_ctx); | ||
} | ||
} | ||
|
||
@Override | ||
public void onStop(Object component) { | ||
if (m_initialized.get()) { | ||
if (component instanceof Disposable) { | ||
((Disposable) component).dispose(); | ||
} | ||
} else { | ||
throw new IllegalStateException("Component lifecycle has been shutdown!"); | ||
if (component instanceof Disposable) { | ||
((Disposable) component).dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 49 additions & 5 deletions
54
cat-client/src/test/java/com/dianping/cat/CatBootstrapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,60 @@ | ||
package com.dianping.cat; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CatBootstrapTest { | ||
import com.dianping.cat.configuration.ConfigureManager; | ||
|
||
public class CatBootstrapTest extends ComponentTestCase { | ||
@After | ||
public void after() { | ||
} | ||
|
||
@Before | ||
public void before() { | ||
} | ||
|
||
@Test | ||
public void testInitializeByDomain() { | ||
Cat.getBootstrap().initializeByDomain("MyDomain"); | ||
|
||
ConfigureManager manager = context().lookup(ConfigureManager.class); | ||
|
||
Assert.assertEquals("MyDomain", manager.getDomain()); | ||
} | ||
|
||
@Test | ||
public void testLazyInitialize() { | ||
Cat.newTransaction("Type", "Name"); | ||
public void testInitializeByDomainAndServers() { | ||
Cat.getBootstrap().initializeByDomain("MyDomain", "server1", "server2"); | ||
|
||
ConfigureManager manager = context().lookup(ConfigureManager.class); | ||
|
||
Assert.assertEquals("MyDomain", manager.getDomain()); | ||
Assert.assertEquals(2, manager.getServers().size()); | ||
Assert.assertEquals("server1", manager.getServers().get(0).getIp()); | ||
Assert.assertEquals("server2", manager.getServers().get(1).getIp()); | ||
} | ||
|
||
@Test | ||
public void testInitializeByServer() { | ||
Cat.getBootstrap().initialize("localhost"); | ||
public void testInitializeByServers() { | ||
Cat.getBootstrap().initialize("server1", "server2"); | ||
|
||
ConfigureManager manager = context().lookup(ConfigureManager.class); | ||
|
||
Assert.assertEquals(2, manager.getServers().size()); | ||
Assert.assertEquals("server1", manager.getServers().get(0).getIp()); | ||
Assert.assertEquals("server2", manager.getServers().get(1).getIp()); | ||
} | ||
|
||
@Test | ||
public void testLazyInitialization() { | ||
Assert.assertEquals(false, Cat.getBootstrap().isInitialized()); | ||
|
||
// CAT API call will trigger lazy initialization | ||
Cat.newTransaction("Type", "Name").success().complete(); | ||
|
||
Assert.assertEquals(true, Cat.getBootstrap().isInitialized()); | ||
} | ||
} |
Oops, something went wrong.