001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package net.tridentsdk; 019 020import com.google.common.base.Preconditions; 021import net.tridentsdk.config.Config; 022import net.tridentsdk.docs.InternalUseOnly; 023 024import javax.annotation.concurrent.ThreadSafe; 025import java.io.File; 026import java.net.InetAddress; 027import java.nio.file.Path; 028import java.nio.file.Paths; 029 030/** 031 * Utility static accessor which delegates to the {@link Server} 032 * 033 * @author The TridentSDK Team 034 * @since 0.3-alpha-DP 035 */ 036@ThreadSafe 037public final class Trident { 038 private static final ExposedSecurity SECURITY = new ExposedSecurity(); 039 private static class ExposedSecurity extends SecurityManager { 040 @Override 041 protected Class[] getClassContext() { 042 return super.getClassContext(); 043 } 044 } 045 046 private static volatile Server server; 047 048 private Trident() { 049 } 050 051 /** 052 * Gets the server singleton that is currently running 053 * 054 * @return the server that is running 055 */ 056 public static Server instance() { 057 return server; 058 } 059 060 /** 061 * Do not call 062 * 063 * <p>Will throw an exception if you are not calling from a trusted source</p> 064 * 065 * @param s the server to set 066 */ 067 @InternalUseOnly 068 public static void setServer(Server s) { 069 Preconditions.checkState(isTrident(), "Server instance can only be set by TridentSDK!"); 070 server = s; 071 } 072 073 @InternalUseOnly 074 public static boolean isTrident() { 075 return findCaller(3).getClassLoader().equals(Trident.class.getClassLoader()); 076 } 077 078 @InternalUseOnly 079 public static Class<?> findCaller(int index) { 080 return SECURITY.getClassContext()[index]; 081 } 082 083 /** 084 * Returns the server's working directory, with the file separator appended 085 * 086 * @return the server working directory 087 */ 088 public static Path fileContainer() { 089 return Paths.get(System.getProperty("user.dir") + File.separator); 090 } 091 092 /** 093 * The information displayed on the client server list when pinged 094 * 095 * @return the display information 096 */ 097 public static PingInfo info() { 098 return server.info(); 099 } 100 101 /** 102 * The port which the server connection has been opened on 103 * 104 * @return the port number 105 */ 106 public static int port() { 107 return server.port(); 108 } 109 110 /** 111 * Stops the server 112 */ 113 public static void shutdown() { 114 server.shutdown(); 115 } 116 117 /** 118 * Obtains the IP of the server 119 * 120 * @return the server IP 121 */ 122 public static InetAddress ip() { 123 return server.ip(); 124 } 125 126 /** 127 * Obtains the server release version 128 * 129 * @return the server release version 130 */ 131 public static String version() { 132 return server.version(); 133 } 134 135 /** 136 * Obtains access to the server console 137 * 138 * @return the console 139 */ 140 public static Console console() { 141 return server.console(); 142 } 143 144 /** 145 * The server configuration file 146 * 147 * @return the config 148 */ 149 public static Config config() { 150 return server.config(); 151 } 152 153 /** 154 * The singleton instance of the server 155 * 156 * @return the server instance 157 */ 158 public static Server server() { 159 return server; 160 } 161}