use of org.wso2.ballerinalang.compiler.util.Names in project ballerina by ballerina-lang.
the class BallerinaDocGenerator method generatePackageDocsFromBallerina.
/**
* Generates {@link BLangPackage} objects for each Ballerina package from the given ballerina files.
*
* @param sourceRoot points to the folder relative to which package path is given
* @param packagePath a {@link Path} object pointing either to a ballerina file or a folder with ballerina files.
* @param packageFilter comma separated list of package names/patterns to be filtered from the documentation.
* @param isNative whether the given packages are native or not.
* @return a map of {@link BLangPackage} objects. Key - Ballerina package name Value - {@link BLangPackage}
*/
protected static Map<String, BLangPackage> generatePackageDocsFromBallerina(String sourceRoot, Path packagePath, String packageFilter, boolean isNative) throws IOException {
final List<Path> packagePaths = new ArrayList<>();
if (Files.isDirectory(packagePath)) {
BallerinaSubPackageVisitor subPackageVisitor = new BallerinaSubPackageVisitor(packagePath, packagePaths);
Files.walkFileTree(packagePath, subPackageVisitor);
} else {
packagePaths.add(packagePath);
}
BallerinaDocDataHolder dataHolder = BallerinaDocDataHolder.getInstance();
if (!isNative) {
// This is necessary to be true in order to Ballerina to work properly
System.setProperty("skipNatives", "true");
}
BLangPackage bLangPackage;
for (Path path : packagePaths) {
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(CompilerOptionName.PROJECT_DIR, sourceRoot);
options.put(CompilerOptionName.COMPILER_PHASE, CompilerPhase.DESUGAR.toString());
options.put(CompilerOptionName.PRESERVE_WHITESPACE, "false");
Compiler compiler = Compiler.getInstance(context);
// TODO: Remove this and the related constants once these are properly handled in the core
if (BAL_BUILTIN.equals(path) || BAL_BUILTIN_CORE.equals(path)) {
bLangPackage = loadBuiltInPackage(context);
} else {
// compile the given file
bLangPackage = compiler.compile(getPackageNameFromPath(path));
}
if (bLangPackage == null) {
out.println(String.format("docerina: invalid Ballerina package: %s", packagePath));
} else {
String packageName = bLangPackage.symbol.pkgID.name.value;
if (isFilteredPackage(packageName, packageFilter)) {
if (BallerinaDocUtils.isDebugEnabled()) {
out.println("Package " + packageName + " excluded");
}
continue;
}
dataHolder.getPackageMap().put(packageName, bLangPackage);
}
}
return dataHolder.getPackageMap();
}
use of org.wso2.ballerinalang.compiler.util.Names in project ballerina by ballerina-lang.
the class IdentifierLiteralServiceTest method testUsingIdentifierLiteralsInServiceAndResourceNames.
@Test(description = "Test using identifier literals in service and resource names", enabled = false)
public void testUsingIdentifierLiteralsInServiceAndResourceNames() {
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/identifierLiteral/resource", "GET");
HTTPCarbonMessage response = Services.invokeNew(application, cMsg);
Assert.assertNotNull(response);
BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertEquals(bJson.value().get("key").asText(), "keyVal");
Assert.assertEquals(bJson.value().get("value").asText(), "valueOfTheString");
}
use of org.wso2.ballerinalang.compiler.util.Names in project ballerina by ballerina-lang.
the class HTTPSessionEssentialMethodsTest method testCheckPathValidityPerService.
@Test(description = "Test for path limitation per service")
public void testCheckPathValidityPerService() {
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/sample2/names", "GET");
HTTPCarbonMessage response = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, cMsg);
Assert.assertNotNull(response);
String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
;
Assert.assertNotNull(responseMsgPayload);
Assert.assertEquals(responseMsgPayload, "arraysize:2");
String cookie = response.getHeader(RESPONSE_COOKIE_HEADER);
String sessionId = CookieUtils.getCookie(cookie).value;
cMsg = MessageUtils.generateHTTPMessage("/sample2/names3", "GET");
cMsg.setHeader(COOKIE_HEADER, SESSION_ID + sessionId);
response = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, cMsg);
Assert.assertNotNull(response);
responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertNotNull(responseMsgPayload);
Assert.assertEquals(responseMsgPayload, "4");
}
use of org.wso2.ballerinalang.compiler.util.Names in project carbon-apimgt by wso2.
the class APIUtil method getExternalStores.
/**
* Returns a set of External API Stores as defined in the underlying governance
* registry.
*
* @return a Map of tier names and Tier objects - possibly empty
* @throws APIManagementException if an error occurs when loading tiers from the registry
*/
public static Set<APIStore> getExternalStores(int tenantId) throws APIManagementException {
// First checking if ExternalStores are defined in api-manager.xml
Set<APIStore> externalAPIStores = getGlobalExternalStores();
// If defined, return Store Config provided there.
if (externalAPIStores != null && !externalAPIStores.isEmpty()) {
return externalAPIStores;
}
// Else Read the config from Tenant's Registry.
externalAPIStores = new HashSet<>();
try {
Iterator apiStoreIterator = getExternalStoresIteratorFromConfig(tenantId);
if (apiStoreIterator != null) {
while (apiStoreIterator.hasNext()) {
APIStore store = new APIStore();
OMElement storeElem = (OMElement) apiStoreIterator.next();
String type = storeElem.getAttributeValue(new QName(APIConstants.EXTERNAL_API_STORE_TYPE));
String className = storeElem.getAttributeValue(new QName(APIConstants.EXTERNAL_API_STORE_CLASS_NAME));
store.setPublisher((APIPublisher) getClassInstance(className));
// Set Store type [eg:wso2]
store.setType(type);
String name = storeElem.getAttributeValue(new QName(APIConstants.EXTERNAL_API_STORE_ID));
if (name == null) {
log.error("The ExternalAPIStore name attribute is not defined in external-api-stores.xml.");
}
// Set store name
store.setName(name);
OMElement configDisplayName = storeElem.getFirstChildWithName(new QName(APIConstants.EXTERNAL_API_STORE_DISPLAY_NAME));
String displayName = (configDisplayName != null) ? replaceSystemProperty(configDisplayName.getText()) : name;
// Set store display name
store.setDisplayName(displayName);
store.setEndpoint(replaceSystemProperty(storeElem.getFirstChildWithName(new QName(APIConstants.EXTERNAL_API_STORE_ENDPOINT)).getText()));
// Set store endpoint, which is used to publish APIs
store.setPublished(false);
if (APIConstants.WSO2_API_STORE_TYPE.equals(type)) {
OMElement password = storeElem.getFirstChildWithName(new QName(APIConstants.EXTERNAL_API_STORE_PASSWORD));
if (password != null) {
String value = password.getText();
PasswordResolver passwordResolver = PasswordResolverFactory.getInstance();
store.setPassword(replaceSystemProperty(passwordResolver.getPassword(value)));
store.setUsername(replaceSystemProperty(storeElem.getFirstChildWithName(new QName(APIConstants.EXTERNAL_API_STORE_USERNAME)).getText()));
// Set store login username
} else {
log.error("The user-credentials of API Publisher is not defined in the <ExternalAPIStore> " + "config of external-api-stores.xml.");
}
}
externalAPIStores.add(store);
}
}
} catch (ClassNotFoundException e) {
String msg = "One or more classes defined in APIConstants.EXTERNAL_API_STORE_CLASS_NAME cannot be found";
throw new APIManagementException(msg, e);
} catch (InstantiationException e) {
String msg = "One or more classes defined in APIConstants.EXTERNAL_API_STORE_CLASS_NAME cannot be load";
throw new APIManagementException(msg, e);
} catch (IllegalAccessException e) {
String msg = "One or more classes defined in APIConstants.EXTERNAL_API_STORE_CLASS_NAME cannot be access";
throw new APIManagementException(msg, e);
}
return externalAPIStores;
}
use of org.wso2.ballerinalang.compiler.util.Names in project carbon-apimgt by wso2.
the class APIUtilTierTest method testGetAvailableTiersWithNonExisting.
@Test
public void testGetAvailableTiersWithNonExisting() throws Exception {
Map<String, Tier> definedTiers = getDefinedTiers();
// Select valid tier names to be assigned to the API
Set<Tier> expectedTiers = getRoundRobinTierString(validTierNames);
Set<Tier> assignedTiers = new HashSet<Tier>(expectedTiers);
assignedTiers.add(new Tier("Bogus"));
String apiName = "testApi";
Set<Tier> availableTiers = APIUtil.getAvailableTiers(definedTiers, getTiersAsString(assignedTiers), apiName);
Assert.assertEquals("Expected tiers do not match", expectedTiers, availableTiers);
}
Aggregations